Android Code Snippet Add graphic to EditText

SubName: EditTextSearch

Description: Add a graphic (search icon in this case) to an EditText

B4X:
Sub EditTextSearch(EditTxt As EditText)
    Dim AR As AndroidResources
    Dim ETxt As JavaObject = EditTxt
    ETxt.RunMethod("setCompoundDrawablesWithIntrinsicBounds",Array As Object(AR.GetAndroidDrawable("ic_menu_search"),Null,Null,Null))
End Sub

To set the size of a graphic you can set the bounds before adding to the View:

B4X:
Sub setButtonImage(Btn As Button,Img As BitmapDrawable)
    Dim R As Rect
    R.Initialize(0,0,40Dip,40Dip)
    Dim ImgJO As JavaObject = Img
    ImgJO.RunMethod("setBounds",Array As Object(R))

    Dim Btn As JavaObject = btnFileItem
    Btn.RunMethod("setCompoundDrawables",Array As Object(Img,Null,Null,Null))
End Sub

For more info and variations see the documentation:http://developer.android.com/refere...wablesWithIntrinsicBounds(int, int, int, int)

Depends on JavaObject, AndroidResources

Tags: EditText Graphic Icon
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Added another example with image sizing
 

DonManfred

Expert
Licensed User
Longtime User
Top