I have two ImageButtons, each with a different image within. I need to find Java code which essentially allows ImageButton2 to display ImageButton1's image. I've been flailing around, but I think the magic command should be something like this:
ImageButton2.setImageResource( ImageButton1.getImageAlpha() );
ImageButton2.setImageResource( ((BitmapDrawable) ImageButton1.getDrawable()).getBitmap() );
int id = ImageButton1.getId();
ImageButton2.setImageResource( R.drawable.(id) );
But none of these compile. The answer has to be something like this:
ImageButton2.setImageResource( ImageButton1.getImageResource() );
Anyone see the solution? I've been working on this all day. Thanks.
That can be achieved following way:
Drawable drawable = imageButton1.getDrawable();
Drawable mutatedDrawable = drawable.mutate();
imageButton2.setImageDrawable(mutatedDrawable);
By default, drawables would be shared. If you want changes made one drawable not to affect to the same drawable attached to another view, then you have to explicitly mutate()
the drawable.