implement android:src="@drawable/image" programmatically in Android

75,569

Solution 1

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);

Solution 2

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!

Solution 3

Hope ths will help you

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

Solution 4

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) {
    }

Solution 5

One more short variant

views.setImageViewResource(R.id.button1, R.drawable.newbutton);
Share:
75,569
user788511
Author by

user788511

Updated on July 09, 2022

Comments

  • user788511
    user788511 almost 2 years

    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.

  • user788511
    user788511 over 12 years
    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
    user788511 over 12 years
    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
    user788511 over 12 years
    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??
  • Android Killer
    Android Killer over 12 years
    I think you can make a bitmap object from that image and use button1.img.setImageBitmap(yuorbitmapobject);
  • Nikunj Patel
    Nikunj Patel over 12 years
    by this you can get image from particular url
  • user788511
    user788511 over 12 years
    This is super Marco, however I am only limited to drawables..this is the design specification..I can not use bitmaps
  • user788511
    user788511 over 12 years
    This is superb Nik, however I am working with drawables not bitmaps, is there a workaround for this??
  • Marco
    Marco over 12 years
    @user788511: if you're limited to drawables, what does your server returns you?
  • Nikunj Patel
    Nikunj Patel over 12 years
    basically it will take any image in bitmap format and set on button. trust me try and vote me
  • Nikunj Patel
    Nikunj Patel over 12 years
    take any temp url and past in code and try it out but make sure in manifest you need to make Internet permission
  • user788511
    user788511 over 12 years
    the server returns image which is converted to drawable at runtime
  • Marco
    Marco over 12 years
    @user788511: Where do you save that runtime converted image? Which android type is your converted image?
  • user788511
    user788511 over 12 years
    I implement it like this:normalImage = Drawable.createFromStream(code)
  • Marco
    Marco over 12 years
    @user788511: take a look at my edited answer and let me know if this works.
  • user788511
    user788511 over 12 years
    Marco, thank you for the help, however I receive a BitmapDrawable can not be resolved to a type error!! Is there a remedy?
  • Marco
    Marco over 12 years
    @user788511: did you import correct library? Do you have this error at compile-time or at run-time?
  • Marco
    Marco over 12 years
    @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!
  • user788511
    user788511 over 12 years
    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
  • Marco
    Marco over 12 years
    @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?
  • user788511
    user788511 over 12 years
    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..
  • Marco
    Marco over 12 years
    @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 !!!
  • stevehs17
    stevehs17 about 8 years
    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).
  • FrankKrumnow
    FrankKrumnow over 5 years
    Drawable d = new BitmapDrawable(getResources(), bitmap_skip );