1

I have a number of Buttons that are dynamically added to a GridView using an Adapter. These currently fit inside the GridView correctly, but...

I would like to add an image inside each button. I have banana.jpg stored in my drawable folder and I use the following code to set it inside the button:

button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.banana));

However, I have two problems:

  1. The buttons seem to stretch to the size of the image.
  2. The image doesn't actually appear.

Is there some way to add an image to a button such that the current size of the button will scale the image down so that it fits? (Or, is it possible to calculate the size I will need the image to be so I can resize it manually?)

sdasdadas
  • 23,917
  • 20
  • 63
  • 148

1 Answers1

1

You have to use ImageButton instead of Button,

If you want to use Button may be This answer can help you.

Community
  • 1
  • 1
Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70
  • I've been using `Button` so I can use text alongside the image. – sdasdadas Mar 15 '13 at 03:23
  • and what about the answer on the link? – Shajeel Afzal Mar 15 '13 at 03:29
  • I am trying it using: `Drawable icon = context.getResources().getDrawable(R.drawable.banana); button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);` but it doesn't show any image on my buttons when I run the application. It's just a blank button (and the text is now missing). – sdasdadas Mar 15 '13 at 03:31
  • Ah there seems to be something wrong with my drawable/Eclipse/.jpg. Your solution works with the built in Android icons. Thanks! – sdasdadas Mar 15 '13 at 03:36