0

i need to set background resource for my button from another button

I am trying it, but it doesn't work

button1.setBackground(button2.getBackground());
Leomund
  • 145
  • 1
  • 1
  • 9
  • 1
    Check this thread http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android – Gordak Jun 30 '15 at 10:51

1 Answers1

0

Try this code

        final Drawable backgroundDrawable = button2.getBackground();
        if (backgroundDrawable instanceof BitmapDrawable) {
            button1.setBackgroundDrawable(backgroundDrawable);
        }else if (backgroundDrawable instanceof ColorDrawable) {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                          final ColorDrawable colorDrawable = (ColorDrawable)   backgroundDrawable;
                       button1.setBackgroundColor(colorDrawable.getColor());
            }
         }

Hope it will help.

M.Khouli
  • 3,992
  • 1
  • 23
  • 26