48

I am trying to set the foreground image on an image button. After some research, I came across this code sample:

<ImageButton android:text="Button" android:id="@+id/button1"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:src="@drawable/icon"/>

My query is how to actually implement android:src in code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user788511
  • 1,726
  • 2
  • 30
  • 52

6 Answers6

111

Try this:

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageResource(R.drawable.newimage);

where newimage is the image name in drawable folder.

EDITED
try this:

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageBitmap(bm);

where bm is bitmap extracted from server.

EDITED AGAIN
I see you receive a Drawable; well, do this:

normalImage = Drawable.createFromStream(code);
Bitmap bm = ((BitmapDrawable)normalImage).getBitmap();
ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageBitmap(bm);
Marco
  • 56,740
  • 14
  • 129
  • 152
  • Thanks alot.. however, I am getting the image from the server and not storing it in drawable folder, is there any way of achieving this?? – user788511 Sep 08 '11 at 11:12
  • This is super Marco, however I am only limited to drawables..this is the design specification..I can not use bitmaps – user788511 Sep 08 '11 at 11:18
  • @user788511: if you're limited to drawables, what does your server returns you? – Marco Sep 08 '11 at 11:22
  • the server returns image which is converted to drawable at runtime – user788511 Sep 08 '11 at 11:27
  • @user788511: Where do you save that runtime converted image? Which android type is your converted image? – Marco Sep 08 '11 at 11:32
  • I implement it like this:normalImage = Drawable.createFromStream(code) – user788511 Sep 09 '11 at 01:51
  • @user788511: take a look at my edited answer and let me know if this works. – Marco Sep 09 '11 at 06:04
  • Marco, thank you for the help, however I receive a BitmapDrawable can not be resolved to a type error!! Is there a remedy? – user788511 Sep 09 '11 at 06:39
  • @user788511: did you import correct library? Do you have this error at compile-time or at run-time? – Marco Sep 09 '11 at 06:42
  • @user788511: use this `import android.graphics.drawable.BitmapDrawable;`. Anyway if you use Eclipse you can hit CTRL+SHIFT+M over _BitmapDrawable_ and the import is done automatically! – Marco Sep 09 '11 at 06:55
  • Marco, I have tried this but still not progressing, is there a way I can convert this drawable to a resource so that I can use setImageResource()?? Thank you for all the help – user788511 Sep 09 '11 at 07:01
  • @user788511: _still not progressing_ doesn't help us to understand which problem you're facing. Explain better: after you inserted correct import and modified code, what's happening? 1. Do you receive correctly your drawable from server? 2. Is your drawable converted to bitmap? – Marco Sep 09 '11 at 07:04
  • I apologize.. After I fix the imports and run the code, the image is not displayed on the foreground as with setImageResource(), it is displayed in the background.. – user788511 Sep 09 '11 at 07:06
  • @user788511: close this question and accept an answer, believe me!! You told us you couldn't set the image and we all told you dozens of methods to do it, helped you with code... and you tell us that yes, your image is shown but in the background... come on, google around or ask another question on SO to know how to show this image in front of your button !!! – Marco Sep 09 '11 at 07:12
14

Here's what worked for me in setting the image:src on an ImageButton programmatically** or through code:

1.Retrieve the image drawable.

Drawable tempImage = getResources().getDrawable(R.drawable.my_image);

2.Get the view.

ImageButton tempButton = (ImageButton)findViewById(R.id.my_image_button);

3.Set the image for the view.

tempButton.setImageDrawable(tempImage);

Hope this works for you too!

David
  • 15,894
  • 22
  • 55
  • 66
KarenAnne
  • 2,764
  • 1
  • 28
  • 21
  • 2
    Very clear! Note, though, that step 1 uses an API that is deprecated as of API Level 22. I replaced it with: ContextCompat.getDrawable(context, R.drawable.my_image). – stevehs17 Feb 05 '16 at 20:53
4

Hope ths will help you

ImageButton button1=(ImageButton)findViewById(R.id.button1);       
button1.setImageResource(R.drawable.icon);
Marco
  • 56,740
  • 14
  • 129
  • 152
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • Thanks alot.. however, I am getting the image from the server and not storing it in drawable folder, is there any way of achieving this?? – user788511 Sep 08 '11 at 11:14
  • I think you can make a bitmap object from that image and use button1.img.setImageBitmap(yuorbitmapobject); – Android Killer Sep 08 '11 at 11:17
3

try this::

ImageButton tran_btn_skip;

tran_btn_skip = (ImageButton) findViewById(R.id.btn);
    try {
        Bitmap bitmap_skip = BitmapFactory.decodeStream((InputStream) new URL(
                "http://233.129.115.55/MRESC/images/test/skip.png")
                .getContent());
        tran_btn_skip.setImageBitmap(bitmap_skip);
    } catch (Exception e) {
    }
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
2

One more short variant

views.setImageViewResource(R.id.button1, R.drawable.newbutton);
Joss
  • 106
  • 1
  • 2
  • 8
0

I know this is an old question, but for future searches... I believe what you are looking for is:

imgButton.setImageDrawable(drawable);
Mick0311
  • 161
  • 4
  • 11