Piofmc
Piofmc

Reputation: 365

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.

Upvotes: 4

Views: 11490

Answers (5)

Gopal Gopi
Gopal Gopi

Reputation: 11131

You need to set the ID of drawable resource but you are setting ID of the ImageButton as image resource.

here stuff.getId() returns R.id.stuff.

set the valid drawable resource like

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
stuff.setImageResource(R.drawable.id_of_image);

Upvotes: 0

u.jegan
u.jegan

Reputation: 833

This will help get the drawable object and set the drawable object.You can also get the Bitmap object for image drawable This will set the exact drawable associated with the imagebutton

ImageButton mButton = new ImageButton(this);
Drawable drawable = mButton.getBackground();
champs.setImageDrawable(drawable)

Upvotes: 0

Sonali8890
Sonali8890

Reputation: 2005

Try this

ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
Drawable d = stuff .getBackground();
champs.setBackgroundDrawable(d);

Upvotes: 0

Anil kumar
Anil kumar

Reputation: 1534

try this

Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();

Upvotes: 9

Triode
Triode

Reputation: 11357

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

Upvotes: 1

Related Questions