Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from an ImageButton?

I'm not sure about the code, but I tried doing this:

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
int stuff2 = stuff.getId();
champs.setImageResource(stuff2);

But that didn't work, the Imagebutton was shrinked for some reason, there was no image, just a gray rectangle.

like image 569
Piofmc Avatar asked Dec 25 '22 13:12

Piofmc


2 Answers

try this

Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();
like image 169
Anil kumar Avatar answered Jan 13 '23 00:01

Anil kumar


stuff.getId();

This will return the id of the View not the image resource associated with it. So you won't be having a resource related to this that is why you are not seeing the Image.

Set a valid drawable to setImageResource Some thing like R.drawable.drawable_id

like image 39
Triode Avatar answered Jan 12 '23 23:01

Triode