1

if you take a look at this link as an excample http://www.mkyong.com/android/android-imagebutton-example/

My question is How can I get the ImageButton Background Color when the user clicked on that ImageButton?

Thanks.

Instead of showing a message...

       Toast.makeText(MyAndroidAppActivity.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

I would like to get the color value, so I can use it to change another control background color. Think of it as a Color Picker Dialogbox.

1 Answers1

0

You cannot retrieve the color.

The background of a button is a Drawable:

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

if you know that the background is a color, you should try:

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
  • I've seen this post before and I've tried it. It crashed the app when it tried to cast the object to Button, BUT, I've just realized what I did wrong :-) I should have cast it to ImageButton So this is what I did and it Worked!!! Thank you all. I love StackExchange aka StackOverflow. You guys and gals rock!!! ImageButton imageButton = (ImageButton) findViewById(resID); ColorDrawable buttonColor = (ColorDrawable) imageButton.getBackground(); – FearlessCoder Jan 29 '15 at 19:48
  • One more question. This getColor() method is not available in API 10 which I am targeting. So is there a different way of doing this for API 10? – FearlessCoder Jan 29 '15 at 20:05
  • No, it is not possible =( getColor() was introduced in Android 3.0 please mark the question as correct =) – Sandro Machado Jan 29 '15 at 23:52