I want to add two images to a button that has two lines of text, like so:
Note: the above image is photoshopped to look like what I want.
I am not sure how to achieve this. I can programmatically add one image to the button on the left like so:
Button button = new Button(this);
int imgResource = R.drawable.titans;
String matchUpStr = "TitansnPatriots";
button.setGravity(Gravity.START);
button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
button.setText(matchUpStr);
but it just ends up like this:
Does anyone know how I could add the two images to the left of the text in the button? I am thinking maybe you can go to the next line on the imgResource variable and add another drawable. Not sure.
note that I will be dynamically creating buttons and it is not always going to be this matchup. So I can’t just add an image with these two teams on it.
I’m starting to think that maybe instead of adding two images along with two lines of text, why not just make two images that contain the text inside the images and add the images top and bottom.
2
Answers
The only solution that comes to mind is place a Button inside a relative layout (or any layout you prefer) match parent then organize the view over it (Just make sure that on api 21+ the button has elevation set it to 0 or something).
This link inspired me: Android Custom button with imageview and textview inside?
Although I agree with Nero that a custom button is probably a cleaner solution.
I was able to come up with a solution using a LayerListDrawable.
I resized both bitmap drawables to be the size of the button’s text and then inset them by that size in the layer list drawable. See the code example.
I was able to conjure the following using my above code snippet.
It is not 100%, but maybe a path that you can take to accomplish your goal.
Layer List Drawable Screenshot