RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

How to retrieve an image from ImageView when Click on that image?

i have a ImageView in the layout, when i click on the image i want to get that image into a variable and replace with another image in this ImageView. please help me..

Upvotes: 1

Views: 7995

Answers (2)

Nikunj Patel
Nikunj Patel

Reputation: 22076

in this example i have take previous image in Drawable and replace i with new image. if you set any imageview to image which stay in drawable variable(d) then use :: setBackgroundDrawable(d); is useful

 public void onClick(View v){
   ImageView i;
        i = (ImageView) findViewById(R.id.img);
        Drawable d = i.getBackground();
        i.setBackgroundResource(R.id.secondImage);

   }

Upvotes: 2

K-ballo
K-ballo

Reputation: 81389

The onClick Listener will give you a View, that's the ImageView that was clicked. Cast it to an ImageView and do whatever you want with it.

Upvotes: 2

Related Questions