vaibvorld
vaibvorld

Reputation: 389

How to know which image is set on ImageView?

I have an ImageView that can have either of two images ( say image 1 & image 2, stored in drawable). If ImageView contains image1 I want click on that ImageView to be disabled and if image 2 is being displayed, on click image should change to image1 & click is disabled on ImageView.
I am unable to find way to know which image is currently being displayed on ImageView.

This is my code

ImageView select = (ImageView) view.findViewById(R.id.select); 
select.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
// TODO Auto-generated method stub 
} 
});

Upvotes: 2

Views: 2051

Answers (7)

Abhay Gupta
Abhay Gupta

Reputation: 21

I faced with the same problem, and i can tell you what I did. I found this most simplest

public class MainActivity extends AppCompatActivity {
ImageView iv;
Button btn;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    iv = findViewById(R.id.imageView);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(i==0)
            {
                iv.setImageResource(R.drawable.baloon); //baloon -> first image
                i=1;
            }
            else
            {
                i = 0;
                iv.setImageResource(R.drawable.sky); //sky -> second image
            }
        }
    });
}

}

NOTE:- Here in xml, I used sky as the initial image of the image view, which is important otherwise our image will not change on first click.

Upvotes: 0

viper
viper

Reputation: 1906

You could use a tag in imageview and check for the image accordingly. Then when you change the image, again set the tag for the image view. An example :-

Here I am showcasing the example for 8 images. I have first set image named p1 from layout file and set the image view tag as 1. Then when the user taps on the image, I check the imageview tag name. And change the image accordingly.

   imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String tagId = imageView.getTag().toString();
            switch (tagId) {
                case "1":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p2));
                    imageView.setTag("2");
                    break;
                case "2":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p3));
                    imageView.setTag("3");
                    break;
                case "3":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p4));
                    imageView.setTag("4");
                    break;
                case "4":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p5));
                    imageView.setTag("5");
                    break;
                case "5":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p6));
                    imageView.setTag("6");
                    break;
                case "6":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p7));
                    imageView.setTag("7");
                    break;
                case "7":
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.p8));
                    imageView.setTag("8");
                    break;

            }

        }
    });

Upvotes: 0

AnujD
AnujD

Reputation: 1

You can use an Integer and change it's value when you change your image.
For example use a

public Integer num; 

when you set image1 on the view, change num's value to 1. and in the onClickListener method check whether num is 1 or not. i.e.

if (num == 1) 

change to image 2 else change to image 1

Upvotes: 0

Lingeshwaran
Lingeshwaran

Reputation: 579

Set id to imageviews like this select.setId(imageArray[i]) for ref see this link and do onclick action what you want in that.

public static int[] imageArray = {
         R.drawable.image1,
         R.drawable.image2

         };
Bitmap bmp= BitmapFactory.decodeResource(getResources(),imageArray[i]);
    select.setImageBitmap(bmp);
    select.setId(imageArray[i]);
    select.setOnClickListener(new OnClickListener() {
     @Override
    public void onClick(View v) {
         if(v.getId()==R.drawable.image1){
                   //here you set image2 to select 
             } 
         else{
                  //here you set image1 to select
         }
    } } );

Upvotes: 0

Basavaraj Hampali
Basavaraj Hampali

Reputation: 3915

This is for button :

final Button whichLane = (Button) reportDialog.findViewById(R.id.which_side_icon);
        // if button is clicked, close the custom dialog
        whichLane.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (whichLane.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.other_side).getConstantState()) ) {
                    Toast.makeText(HomeActivity.this, "text", 10).show();
                }
            }
        });

Upvotes: 1

Benoît Bouré
Benoît Bouré

Reputation: 1037

Maybe you could consider creating your own View that extends CompoundButton. The checked state would be an image and the non-checked state the other.

Upvotes: 0

323go
323go

Reputation: 14274

I think the cleanest approach to that would be to keep track of this state separate from the UI, and have the UI reflect the state you're interested in. I.e., if it's an Enabled/Disabled representation, keep track of whether this is enabled in a boolean with a proper setter method, and have that method update the UI. This may also be a good application for StateListDrawable.

Upvotes: 2

Related Questions