irobotxx
irobotxx

Reputation: 6063

setImageResource for an ImageView from another ImageView

Strange request here, but is there any way you set an imageview to hold the same drawable as another imageview by getting it from the original imageview. what i mean is:

ImageView image1 = new ImageView(this);
          image1.setimageResource(R.drawable.blah1);

ImageView image2 = new ImageView(this);

//now i want something like this, which doesn't work. i  want to get it from image1
image2.setImageResource(image1.getDrawable());

so please how can i achieve this from image1?.. Thank you :)

Upvotes: 7

Views: 4742

Answers (2)

SHAH MD IMRAN HOSSAIN
SHAH MD IMRAN HOSSAIN

Reputation: 2889

For setting Image Resources from another image view

You need to use two methods of ImagView

  • setImageDrawable()
  • getDrawable()

Your code is all perfect except one error

You need to use setImageDrawable() in place of setImageResource()

following is your code implemention using above methods with correction

ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);

ImageView image2 = new ImageView(this);

// just change this to setImageDrawable
image2.setImageDrawable(image1.getDrawable());

Upvotes: 1

P Varga
P Varga

Reputation: 20229

Use setImageDrawable instead of setImageResource

Upvotes: 7

Related Questions