Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

first click change to new image and second click change to old image, android

Tags:

android

this application requires: first click will change image1 to image2 second click will change back to old image (image2 to image1)

image1  = (ImageView)findViewById(R.id.imageView1);
        image1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                image1.setImageResource(R.drawable.a3_01);
                image1.setTag(70);              
            }
        });

this image will set a new tag for the server knows that the picture have been change.

*the code i used is only for the first click and it works. ive just have no idea to make a second click event. can anyone gives me idea of it? much appreciate. thanks.

like image 248
sara brown Avatar asked Feb 02 '26 12:02

sara brown


1 Answers

You could use a boolean to act as a switch for you to flop back and forth with an if statement.

boolean showingFirst = true;
image1  = (ImageView)findViewById(R.id.imageView1);
image1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(showingFirst == true){
            image1.setImageResource(R.drawable.a3_02);
            showingFirst = false;
        }else{
            image1.setImageResource(R.drawable.a3_01);
            image1.setTag(70);
            showingFirst = true;
        }

    }
});
like image 57
FoamyGuy Avatar answered Feb 05 '26 02:02

FoamyGuy