How to put an image inside a text box?

Inman

Well-Known Member
Licensed User
Longtime User
y6tUY0t.jpg


I am trying to put a lens icon within an Edittext to indicate that it is a search box (as in the above screenshot). The following discussion at Stack Overflow mentions a solution. Any idea how to implement it in B4A, using Reflection library or some other way?

How to add an icon image as hint to EditBox in Android - Stack Overflow
 

stevel05

Expert
Licensed User
Longtime User
Hi Theera,

The required method setCompoundDrawablesRelativeWithIntrinsicBounds is available from TextView and is inherited by EditText from there.

Spinner is not a subclass of TexView and does not inherit the method.

The methods that are available are documented in the links above and most should be straightforward to implement via reflection if not already provided in B4A
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I recently made a suggestion/wish for this very thing because I use it in my TabBar library and it works great. Using a 9 patch for a spinner though may be a better option. I also have a custom spinner/combobox in my LineLayout library that is a Label/Textview and should work with that method too. I may eventually add it to one of my libraries in the future, but really hope Erel adds it too.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
image inside edittext

Is there a way to add a custom image inside an edittext, or is this just for the images already in the android operating system?

Any ideas?

Thanks,
Walter
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can use standard images, they just need to be passed as BitMapDrawables:

B4X:
Dim Bmp As Bitmap
Dim Bmpd As BitmapDrawable
Bmp.Initialize(File.DirAssets,"img.png")
Bmpd.Initialize(Bmp)
'Add to edittext1
ST="android.graphics.drawable.Drawable"
Dim R As Reflector
R.Target=EditText1
R.RunMethod4("setCompoundDrawablesWithIntrinsicBounds",Array As Object(Bmpd,Null,Null,Null),Array As String(ST,ST,ST,ST))
 
Last edited:
Upvote 0
Top