ImageButtons Background

0

I have a ImageButton and when I click on it I want the Background of it to pass to another ImageButton .

I have View v of the first ImageButton , and imgB6 is the ImageButton I want to get the background of v, and gives error in v.getBackground() :

findViewById(R.id.imgB6).setBackgroundResource(v.getBackground());

v.getBackground() is giving error I do not know why, since it is a drawable.

v is received per parameter when in method Onclick , and has a corresponding drawable !

I wanted to pass Backgound from v(View) to imgB6(ImageButton) .

    
asked by anonymous 29.01.2017 / 23:13

4 answers

2

Basically you need to put the background of a second button on the clicked button. Having this and using setOnClickListener() the code below rescues the background using the getBackground() method and sets the background of the current button using the setBackground() method. As the documentation >, you can only use setBackground() with the Android API level 16+ version, however you can use setBackgroundDrawable() , which Although obsolete , it still works. Therefore, a condition that verifies the minimum version defined in your project has been inserted. Here's how it would look:

    final ImageButton ib1 = (ImageButton) findViewById(R.id.ib1);
    final ImageButton ib2 = (ImageButton) findViewById(R.id.ib2);

    ib1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Drawable bg = ib1.getBackground();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

                ib2.setBackground(bg);

            } else {
                /* o metodo setBackgroundDrawable está obsoleto,
                porém ainda funciona para que possamos usar*/
                ib2.setBackgroundDrawable(bg);
            }
        }
    });

Note: There may be other ways to do this.

    
30.01.2017 / 01:48
1

getDrawable() returns Drawable while setBackgroundResource(int) has int as input.

Use findViewById(R.id.imgB6).setBackground(v.getBackground());

    
30.01.2017 / 01:47
0

You are trying to use a Drawable as an argument in a method that expects a int .

To change the background you should use setBackground() and use getBackground() to get the background of the other button:

findViewById(R.id.imgB6).setBackground(v.getBackground());

As you are using ImageButton, the code above may not do what you want. It will depend on how the images were assigned to the buttons.

The main difference between a Button and an ImageButton is that it can be associated with an image that is independent of the background. ImageButton inherits from ImageView.

If the images were assigned in xml , using the android:src attribute, you should use setImageDrawable() and getDrawable() :

findViewById(R.id.imgB6).setImageDrawable(v.getDrawable());
Note: Using the android:src attribute is the usual way of assigning the image to an ImageButton, using android:Background is the same as using a Button.

    
30.01.2017 / 12:04
-1

Do the Onclick on your ImageButton and then the other drawable image.

iDdoImagemButton.setBackgroundResource(@drawable/NomeDAIMAGEM);

** Post the code or an image

    
29.01.2017 / 23:24