dilettante
dilettante

Reputation: 3

android change image onclick imageviev

How can I change Image of an Imageview?

I want to get the image associated; if it is img1 I want set the image to img2, if it is img2 I want to set the image to img2.

Upvotes: 0

Views: 5858

Answers (2)

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

first set the tag of imageview in xml to 1

final ImageView imageview = (ImageView) findViewById(R.id.imageView1);

    imageview.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Integer.parseInt(imageview.getTag().toString()) == 1) {
                imageview.setBackgroundResource(R.drawable.image2);

                imageview.setTag(2);

            } else {
                imageview.setBackgroundResource(R.drawable.image1);
                imageview.setTag(1);

            }

        }
});

Upvotes: 1

Avinash Babu
Avinash Babu

Reputation: 6252

I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as @ρяσѕρєя K mentioned in comment.

There are two version of setTag one which takes object as an argument and other takes key and object as an argument

Upvotes: 0

Related Questions