Background of Imagebuttons

Asked

Viewed 148 times

0

I have a ImageButton and when I click on it wanted the Background of him passing to another ImageButton.

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

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

v.getBackground() You’re making a mistake I don’t know why, since it’s a drawable.

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

I wanted to pass the Backgound of v(View) for imgB6(ImageButton).

  • 1

    Do you have any code? Put the error in your question.

  • 3

    Put the xml of the two imageView

  • sao ImageButtons, one comes per parameter to View what I call v, and this View want to pick up the background and put in another ImageButton imgB6. Then findViewById(R.id.imgB6).setBackgroundResource(v.getBackground());

  • @Leandroalmeida the background is color or image?

4 answers

2

Basically you need to put the background a second button on the button clicked. Having this and using the setOnClickListener() the code below rescues the background using the method getBackground() and defines the background of the current button using the method setBackground(). In accordance with the documentation, can only use the setBackground() with the version of Android API level 16+, however it is observed that one can use setBackgroundDrawable(), that despite being obsolete, still works. Therefore, a condition has been inserted that checks the minimum version defined in your project. See 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);
            }
        }
    });

Obs.: There may be other ways to perform this procedure.

  • I want to work from the View, and doing so is not giving me solution.

  • Thank you very much worked! :)

  • @Leandroalmeida cool. I’m glad everything worked out there. If this way worked for you, please validate the answer. If you need anything else, we’re here.

1

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

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

  • error in .setBackground, "call requires api level 16 (Current min 15)"

  • if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) findViewById(R.id.imgB6).setBackground(v.getBackground());

0

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

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

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

Since 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 the xml, through the attribute android:src, should use setImageDrawable() and getDrawable():

findViewById(R.id.imgB6).setImageDrawable(v.getDrawable());

Note: Using the attribute android:src is the usual way to assign the image to an Imagebutton, if using android:Background is like wearing a button.

-1

Do Onclick on your Imagembutton and then arrow the other drawable image.

iDdoImagemButton.setBackgroundResource(@drawable/NomeDAIMAGEM);

**OBS puts the code or an image

  • The basic thing is to understand... the code is too big and you answered what I didn’t want. I have a view of Imagebutton1 and have an ountro Imagebutton6 I want to put the background of Imagebutton1 in Imagebutton6 tem findViewById(R.id.imgB6) to have the view of Imagebutton6 . setBackgroundResource() to clear Imagebutton6 ’s bacground, the error is in getting the background of Buttimageon1 "v.getBackground()"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.