tags:

views:

177

answers:

2

hello

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?

thanks

A: 

You're supposed to use setImageResource instead of setBackgroundResource.

David Hedlund
+1  A: 

with

myImgView.setBackgroundResource(R.drawable.monkey);

you are puttin that monkey in the background

use

  myImgView.setImageResource(R.drawable.monkey);

or

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
Jorgesys
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