Android Question Label with Image

tipallandgo

Member
Licensed User
Longtime User
Hi guys, I'm trying to put an image into a button just like the Cancel and Done buttons in the image below.

CYQs3.png


I was successful in putting the image with this code.

B4X:
Dim r As Reflector
Dim ar As AndroidResources
Dim st As String
r.Target = btnDone
st  = "android.graphics.drawable.Drawable"
r.RunMethod4("setCompoundDrawablesRelativeWithIntrinsicBounds",Array As Object(ar.GetApplicationDrawable("ic_action_accept"),Null,Null,Null),Array As String(st, st, st, st))

The problem is, the image always go to the leftmost part of the button. I tried both "setCompoundDrawablesRelativeWithIntrinsicBounds" and "setCompoundDrawablesWithIntrinsicBounds" methods but the result is the same. What I want to do is that both of the image and the text are in the center of the button. Can you help me please?

EDIT: Sorry, I just realized the thread title is wrong. But anyway, this can be used also for labels.
 
Last edited:

tipallandgo

Member
Licensed User
Longtime User
I tried using the designer and used StatelistDrawable, but the image and the text are overlapping each other in the center. Is there a way to arrange them side-by-side? The images I am using does not include the text "DONE" and "CANCEL" but only the X and checkmark.
 
Last edited:
Upvote 0

tipallandgo

Member
Licensed User
Longtime User
Sorry for the confusion Martin. What you posted is just for labels. What I need is for buttons. Thanks for the reply though.
I resorted to creating a class with a panel holding an imageview and a label.

Thank you for your time guys.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Ah yeah i didn't think about that.
Not sure if a Button supports basic HTML as it's Text property.
 
Upvote 0

tipallandgo

Member
Licensed User
Longtime User
I guess it is possible, since Buttons are subclasses of Labels, I think.

EDIT: I just tried it using the example and it works. I can't click the links though.

I'm having a hard time setting the position of the ImageView and the Label since I set the Label's width to -2. So right now I'm checking the LabelExtras library. Thanks again.

EDIT2: I'm done now. For those who are using the library and wants to get the image from their drawable folders, change the ImageGetter Sub to this:

B4X:
Sub ImageGetter_GetDrawable(Source As String) As Object

Log("ImageGetter1_GetDrawable: "&Source)
  
  'Get the drawable image, do not include the file extension
  Dim bgIcon As BitmapDrawable
  Dim ar As AndroidResources
  
  bgIcon = ar.GetApplicationDrawable(Source)
  LabelExtras1.SetDrawableBounds(bgIcon, 0, 0, 32dip, 32dip)
  Return bgIcon
End Sub
 
Last edited:
Upvote 0
Top