I was wondering if anybody could help me with a method of creating a button that has text and an image. I need the image to be “a part of the text”. For example
(BUTTON) "This is a money symbol : ($)" (BUTTON)
with the $ symbol an image.
I’d like to be able to change the picture in Java at runtime using something like this:
btnMoney.setBackgroundResource(R.drawable.moneypicture);
Any ideas as to how I could implement this?
Assuming you want the image to the left of the button’s text, you can either use the android:drawableLeft attribute in your XML, or use the setCompoundDrawablesWithIntrinsicBounds() method from your code.
Since you want to do this in code, you need to pass in the resource Id for the correct drawable. The parameters for that method are drawables for the left, top, right, and bottom, so for only left, pass in 0 for the last 3 parameters.