How to know which imageview is clicked and change image

set image in imageview android programmatically
android imageview change image onclick
change imageview image on button click android
how to set image in imageview dynamically in android
when i click on imageview how can it show the same image in next activity in android
change image on button click javafx
show image on button click android
create a mobile application to change the image in the image view by using onclick event

updated i try other options and i see a video on youtube with 3 ways of do this and works until i add the logic :

with this code:

public void onClick(final View imageView) {
    ImageView mypic1 = (ImageView) findViewById(R.id.select1);
    ImageView mypic2 = (ImageView) findViewById(R.id.select2);
    ImageView mypic3 = (ImageView) findViewById(R.id.select3);
    ImageView mypic4 = (ImageView) findViewById(R.id.select4);
    ImageView mypic5 = (ImageView) findViewById(R.id.select5);
    ImageView mypic6 = (ImageView) findViewById(R.id.select6);
    View.OnClickListener listener= new OnClickListener() {
        public void onClick(View v) {
            if(imageView instanceof ImageView ){
                imageView.setTag(1);
                if(imageView.getTag().equals(1)){
                    ((ImageView) imageView).setImageResource(R.drawable.old);
                    imageView.setTag(2);
                }else{
                    ((ImageView) imageView).setImageResource(R.drawable.new);
                    imageView.setTag(1);
                }
            }

        }};

    mypic1.setOnClickListener(listener);
    mypic2.setOnClickListener(listener);
    mypic3.setOnClickListener(listener);
    mypic4.setOnClickListener(listener);
    mypic5.setOnClickListener(listener);
    mypic6.setOnClickListener(listener);

}

all compile but don't work, if i add the toast only, works (only the message with the toast text)

modify on click like this

 public void onClick (View v){
            switch (v.getId()) {
                case R.id.mypic1:
                    //do you action against mypic1:
                    break;
                case R.id.mypic2:
                    //do you action against mypic2:
                    break;
                case R.id.mypic3:
                    //do you action against mypic3:
                    break;
                case R.id.mypic4:
                    //do you action against mypic4:
                    break;
                case R.id.mypic5:
                    //do you action against mypic5:
                    break;
                case R.id.mypic6:
                    //do you action against mypic6:
                    break;
            }
        });

Change source image for image view when pressed, You want to do this. ImageButton Demo_button = (ImageButton)findViewById(R.​id.firstimage); // when you click this demo button Demo_button. I have imageview and i whant when user click this imageview the background of this changed this is my code btnImage =(ImageView) findViewById(R.id.image_button); btnImage.setOnClickListener(new

hi you first add this line to be class

public class test implements View.OnClickListener { ImageView mypic; ...}

you must be set var type to be cast

mypic = (ImageView) findViewById(R.id.mypic1);

mypic.setOnClickListener(this);

public void onClick (View v){
    switch (v.getId()) {
        case R.id.mypic1:
            if(mypic.getTag().toString().equals(1)){
                mypic.setTag(2);
            if (Build.VERSION.SDK_INT >= 21)
                mypic.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.newpic));
            else
                mypic.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.newpic,null));
            }else{
            if (Build.VERSION.SDK_INT >= 21)
                mypic.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.oldpic));
            else
                mypic.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.oldpic,null));
                mypic.setTag(1);
            }                
      break;
        case R.id.pic2:
            //do you action against mypic2:
            break;
        case R.id.pic3:
            //do you action against mypic3:
            break;
        case R.id.pic4:
            //do you action against mypic4:
            break;
        case R.id.pic5:
            //do you action against mypic5:
            break;
        case R.id.pic6:
            //do you action against mypic6:
            break;
    }
}

How to Set Image in a Image View on Click in Android Studio, In this article, you will learn how to set an image in an Image View on a click. Step 1. First I used many Image Views and ScrollViews for the  To change the image of an ImageView you can do the following: ImageView imageView = ( ImageView ) findViewById ( R . id . image_view ); imageView . setImageResource ( R . drawable . my_image ); Now, in order to change the image when you click a button, you can do:

in onStart() of Activity of Fragment in which i have added the ImageView add below code.

 override fun onStart() {
    //get all the image view like this 
    ImageView mypic1 = (ImageView) findViewById(R.id.your_imageView_id1)
    ImageView mypic2 = (ImageView) findViewById(R.id.your_imageView_id2)
    ImageView mypic3 = (ImageView) findViewById(R.id.your_imageView_id3)
    ImageView mypic4 = (ImageView) findViewById(R.id.your_imageView_id4)

    .
    .
    .
    .


    //Set onCLickListener like this 
    View.OnClickListener listener= new OnClickListener() {
        public void onClick(View imageView) {
            if(imageView instanceof ImageView ){
            //add all business logic related to your tag and replacing the image of the view
              if(imageView.getTag().equals(1)){
                imageView.setImageResource(R.drawable.picprevius);
                imageView.setTag(2);
              }else{
                imageView.setImageResource(R.drawable.newpic);
                imageView.setTag(1);
              }
           }
        }      
     }
     //add listener to all the image View like this 
     mypic1.setOnClickListener(listener)
     mypic2.setOnClickListener(listener)
     mypic3.setOnClickListener(listener)
     mypic4.setOnClickListener(listener)
     .
     .
     .
     .

  }

Click on Button to Change the Image in ImageView in Android, In this blog you will learn how to make Button click ImageView change the Image in Android is Below. Set different image inside imageview on button click dynamically in android app. Imageview image can be easily replaceable through MainActivity.java coding file because sometimes app developer want to change the imageview image on any button click. So here is the complete step by step tutorial for Change image in imageview programmatically android.

Android fundamentals 04.1: Clickable images, You must store the image for the ImageView in the drawables folder of your project. In this practical, you learn how to use images as elements that the user can tap or click. Create and edit UI elements using the layout editor and XML code. In this tutorial, we didn’t go in deep about dpi and various screen issue, we just use ImageView to display a “png” image, when user click on a button, it will change to another “png” image. P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3. 1. Add Image to Resources

How Do I Change An ImageView Another Image?, I don't know how to change the image on an ImageView and I want to know so I can make a button change the image when clicked. Add click event method on imageView programmatically using dynamic programming coding. SetOnClickListener() function is used to add click events on android widgets. So app developer can easily add setOnClickListener() on imageView. So here is the complete step by step tutorial for Set onclicklistener on imageview in android example.

Android ImageView, ImageButton using Kotlin, Android ImageView Kotlin Code to display image on the screen. We can use Android We've used a when statement to check the type of the View that's clicked to trigger the action. The scale type changes when the ImageView is clicked. Hi, I am trying to figure out how to get the index of an ImageView[] where an image has been clicked. I have a an array of images displayed when clicked the change to another image, but how do I return the index that has been clicked?

Comments
  • where are you adding this code?? in activity/Fragment?
  • i have one activity called SelectImage and there the user can pick 1 from 6 and i need change the selected to other (its the same image but with a green signal)
  • First thing you have to set onClickListener to all Image View
  • you can not set final v.getTag(); to ImageView because different type
  • can you share the code where you are setting the tag! And the answer i posted it should work.
  • i edit the main thread with your code and i have the same error java.lang.IllegalStateException: Could not execute method for android:onClick
  • in you xml write android:onClick="onClick" in each button.
  • thanks but you are using kotling and i have "cannot resolve" in all your code, i have other activities also in java i can't change all
  • if you have 5 minutes please check the main thread i update the code @Anmol
  • @AndresW if the toast is working it mean's the button click's are working fine you have to set tag's properly as the flow of your code is not right.