324

I have an ImageView with a source image set in the xml using the following syntax:

   <ImageView 
      android:id="@+id/articleImg"
      style="@style/articleImgSmall_2"
      android:src="@drawable/default_m" />

Now I need to change this image programmatically. What I need to do is delete the old image and add a new one though. What I have done is this:

myImgView.setBackgroundResource(R.drawable.monkey);

It works but I noticed android stacks the new image on top of the old one (dont ask me how I found out it's not relevant for the discussion :). I definitely need to get rid of the old one before setting the new image.

How can I achieve that?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
nourdine
  • 7,407
  • 10
  • 46
  • 58

8 Answers8

768

Changing ImageView source:

Using setBackgroundResource() method:

  myImgView.setBackgroundResource(R.drawable.monkey);

you are putting that monkey in the background.

I suggest the use of setImageResource() method:

  myImgView.setImageResource(R.drawable.monkey);

or with setImageDrawable() method:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));

*** With new android API 22 getResources().getDrawable() is now deprecated. This is an example how to use now:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));

and how to validate for old API versions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
   } else {
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
}
Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • oh cool ... unfortunately I have just found out that I need to also replace an image set with myImgView.setImageDrawable(img); the code you suggested myImgView.setImageResource(R.drawable.monkey); is not able to do it. how can I get rid of the drawable before rendering the monkey? :) many thanks – nourdine Jun 04 '10 at 14:37
  • 1
    getDrawable(int) is now deprecated and getDrawable(int,theme) is added in API-21. Now what to do? Can you please update your answer? – mubeen Jul 25 '15 at 21:11
  • Call requires API level 16 (current min is 13): `android.widget.ImageView`#setBackground – Iman Marashi Sep 29 '15 at 20:40
  • 2
    `ContextCompat` to get drawable without `getTheme` – Outofdate Jan 14 '16 at 09:56
  • Now, instead of `getResouces().getDrawable(...)`, use `ResourcesCompat.getDrawable(getResources(), ...)`. – Nishant Jalan Oct 01 '21 at 13:53
  • Latest simply use yourImageView.setImageDrawable(ContextCompat.getDrawable(context, yourImage)); – Ashraf Amin Oct 07 '21 at 08:39
72

You're supposed to use setImageResource instead of setBackgroundResource.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 6
    [Comparison of the two](https://stackoverflow.com/questions/7221673/what-is-the-difference-between-imageview-setbackgroundresource-and-imageview-set) – Suragch Apr 06 '18 at 05:19
35
myImgView.setImageResource(R.drawable.monkey);

is used for setting image in the current image view, but if want to delete this image then you can use this code like:

((ImageView) v.findViewById(R.id.ImageView1)).setImageResource(0);

now this will delete the image from your image view, because it has set the resources value to zero.

Perception
  • 79,279
  • 19
  • 185
  • 195
PIR FAHIM SHAH
  • 511
  • 5
  • 6
20

get ID of ImageView as

ImageView imgFp = (ImageView) findViewById(R.id.imgFp);

then Use

imgFp.setImageResource(R.drawable.fpscan);

to set source image programatically instead from XML.

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
16

Supplemental visual answer

ImageView: setImageResource() (standard method, aspect ratio is kept)

enter image description here

View: setBackgroundResource() (image is stretched)

enter image description here

Both

enter image description here

My fuller answer is here.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
12

Or try this one. For me it's working fine:

imageView.setImageDrawable(ContextCompat.getDrawable(this, image));
radu_paun
  • 1,940
  • 21
  • 25
4

If you want to set in imageview an image that is inside the mipmap dirs you can do it like this:

myImageView.setImageDrawable(getResources().getDrawable(R.mipmap.my_picture)

CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
3

Just write a method for changing imageview

public void setImage(final Context mContext, final ImageView imageView, int picture)
{
    if (mContext != null && imageView != null)
    {
        try
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                imageView.setImageDrawable(mContext.getResources().getDrawable(picture, mContext.getApplicationContext().getTheme()));
            } else
            {
                imageView.setImageDrawable(mContext.getResources().getDrawable(picture));
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
ZR Low
  • 51
  • 7