How to detect which imageview was selected/clicked when clicking another button

I am trying to make a program where there are 3 imageviews, 2 buttons named flip and rotate, and a directional pad. The thing I haven't figured out is how to save which imageview was clicked, so currently only a specific one works.

Here is my mainactivity.java:

package com.mdadi.animals;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onClick(View v) {
    ImageView cat = findViewById(R.id.cat);
    int tenDpAsPx = (int) 
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, 
getResources().getDisplayMetrics());


    switch (v.getId()) {
        case R.id.bird:
            findViewById(R.id.bird_desc).setVisibility(View.VISIBLE);
            //Cat & Dog TextViews set to invisible
            findViewById(R.id.cat_desc).setVisibility(View.INVISIBLE);
            findViewById(R.id.dog_desc).setVisibility(View.INVISIBLE);
            break;
        case R.id.cat:
            findViewById(R.id.cat_desc).setVisibility(View.VISIBLE);
            //Bird & Dog TextViews set to invisible
            findViewById(R.id.bird_desc).setVisibility(View.INVISIBLE);
            findViewById(R.id.dog_desc).setVisibility(View.INVISIBLE);
            break;
        case R.id.dog:
            findViewById(R.id.dog_desc).setVisibility(View.VISIBLE);
            //Bird & Cat TextViews set to invisible
            findViewById(R.id.bird_desc).setVisibility(View.INVISIBLE);
            findViewById(R.id.cat_desc).setVisibility(View.INVISIBLE);
            break;
        default:
            break;
    }
    //Check which button is clicked and execute relevant code
    switch (v.getId()) {
        case R.id.centerBtn: //Reset ImgView translation to original position
            cat.setTranslationX(0);
            cat.setTranslationY(0);
            break;
        case R.id.upArrow: //Move ImgView up ^
            cat.setTranslationY(cat.getTranslationY() - tenDpAsPx);
            break;
        case R.id.downArrow: //Move ImgView down v
            cat.setTranslationY(cat.getTranslationY() + tenDpAsPx);
            break;
        case R.id.leftArrow: //Move ImgView to the left <-
            cat.setTranslationX(cat.getTranslationX() - tenDpAsPx);
            break;
        case R.id.rightArrow: //Move ImgView to the right ->
            cat.setTranslationX(cat.getTranslationX() + tenDpAsPx);
            break;
        case R.id.rotateBtn: //Rotate ImgView 90 degrees clockwise
            cat.setRotation(cat.getRotation() + 90);
            break;
        case R.id.flipBtn: //Flip ImgView about its y axis (horizontally)
            cat.setRotationY(cat.getRotationY() + 180);
            break;
        default:
            break;
    }
}
}

activity_main xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
tools:context=".MainActivity">

<Button
    android:id="@+id/rotateBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:onClick="onClick"
    android:text="@string/rotate_button" />

<Button
    android:id="@+id/flipBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:layout_toEndOf="@+id/rotateBtn"
    android:onClick="onClick"
    android:text="@string/flip_button" />

<ImageButton
    android:id="@+id/centerBtn"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_above="@id/downArrow"
    android:layout_toStartOf="@id/rightArrow"
    android:background="@android:color/transparent"
    android:onClick="onClick"
    android:src="@drawable/ic_filter_center_focus_black_24dp" />

<ImageButton
    android:id="@+id/leftArrow"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:layout_toStartOf="@+id/centerBtn"
    android:background="@android:color/transparent"
    android:onClick="onClick"
    android:src="@drawable/ic_arrow_back_black_24dp" />

<ImageButton
    android:id="@+id/rightArrow"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:background="@android:color/transparent"
    android:onClick="onClick"
    android:src="@drawable/ic_arrow_forward_black_24dp" />

<ImageButton
    android:id="@+id/upArrow"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_above="@+id/centerBtn"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="35dp"
    android:background="@android:color/transparent"
    android:onClick="onClick"
    android:src="@drawable/ic_arrow_upward_black_24dp" />

<ImageButton
    android:id="@+id/downArrow"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="35dp"
    android:layout_marginBottom="15dp"
    android:background="@android:color/transparent"
    android:onClick="onClick"
    android:src="@drawable/ic_arrow_downward_black_24dp" />

<ImageView
    android:id="@+id/bird"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:onClick="onClick"
    android:src="@drawable/bird" />

<ImageView
    android:id="@+id/cat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/bird"
    android:layout_marginBottom="100dp"
    android:onClick="onClick"
    android:src="@drawable/cat" />

<ImageView
    android:id="@+id/dog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/cat"
    android:onClick="onClick"
    android:src="@drawable/dog" />

<TextView
    android:id="@+id/bird_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/bird"
    android:text="@string/bird"
    android:visibility="invisible"/>

<TextView
    android:id="@+id/cat_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/cat"
    android:layout_below="@+id/bird"
    android:text="@string/cat"
    android:visibility="invisible"/>

<TextView
    android:id="@+id/dog_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/dog"
    android:layout_below="@+id/cat"
    android:text="@string/dog"
    android:visibility="invisible"/>

</RelativeLayout>

How would I go about saving which imgview was clicked?

What about to create a int named selectedImage and each time you click on an image you give a value to selectedImage exemple : When image one is clicked selectedImage = 1. And in your code check the value of selectedImage and do the job for the adequate image

Android fundamentals 04.1: Clickable images, A Button is a UI element the user can tap or click to perform an action. You can turn any View , such as an ImageView , into a UI element that can be tapped or� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Make your layout like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="3dp">
    <ImageView
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:onClick="myfunction"/>
</LinearLayout>

So i guess you just want to track which imageView was clicked. So for that, what you can do is create an integer value and then store the index of the image over there.

For example, if you have 3 images then:

public void myfunction(View view){
  switch(view.getParent()){
//tells you which linearlayout was clicked
}
}

You can track later which image was clicked by accessing this variable. Also if you want to even keep the track of the images being clicked then you can use an ArrayList of Integer and then just keep adding the values there.

4.1: Buttons and clickable images � GitBook, Another example is that when you first enter a room, you expect the light switch to Click the Material Icon radio button, and then click the Choose button to (no text), use the ImageButton class, which extends the ImageView class. Use the findViewById() method of the View class to find the Button in the XML layout file: Android :: Radio Button Selection Changes Toast Apr 29, 2010. I was writing a simple test application. There are two radio buttons within the app. There id's are "radio_red" and "radio_blue". I wanted to create an onClickListener event that read the text associated to the button and then returned a basic "Right" or "Wrong" toast.So far no joy.

To detect imageview on button click add selected imageviews in arraylist and on button click get all imageview id and tag from arraylist like this:

Code

Add views on imageclick

 case R.id.img_2:
            if (!imgviewlist.contains(img_2)) {
                imgviewlist.add(img_2);
            }
            break;

getviews on button click

  case R.id.btn_1:
            /*get selected images form array*/
            for (int j = 0; j < imgviewlist.size(); j++) {
                ImageView imageView = imgviewlist.get(j);
                Log.e("get_imageview_id", "" + imageView.getId());
                Log.e("get_imageview_tag", "" + imageView.getTag());
            }
            break;

Full Code

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
Button btn_1;
ImageView img_3, img_2, img_1;
ArrayList<ImageView> imgviewlist = new ArrayList<>();

@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    /*intilize views*/
    btn_1 = findViewById(R.id.btn_1);
    img_3 = findViewById(R.id.img_3);
    img_2 = findViewById(R.id.img_2);
    img_1 = findViewById(R.id.img_1);
    /*set on click listener*/
    btn_1.setOnClickListener(this);
    img_3.setOnClickListener(this);
    img_2.setOnClickListener(this);
    img_1.setOnClickListener(this);

    /*set tag*/
    img_1.setTag("img_1");
    img_2.setTag("img_2");
    img_3.setTag("img_3");

}


@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_1:
            /*get selected images form array*/
            for (int j = 0; j < imgviewlist.size(); j++) {
                ImageView imageView = imgviewlist.get(j);
                Log.e("get_imageview_id", "" + imageView.getId());
                Log.e("get_imageview_tag", "" + imageView.getTag());
            }
            break;
        case R.id.img_1:
            //check item already exist or not
            if (!imgviewlist.contains(img_1)) {
                imgviewlist.add(img_1);  //add imageview in array
            }

            break;
        case R.id.img_2:
            if (!imgviewlist.contains(img_2)) {
                imgviewlist.add(img_2);
            }
            break;
        case R.id.img_3:
            if (!imgviewlist.contains(img_3)) {
                imgviewlist.add(img_3);
            }
            break;

    }
}
 }

Other preprocessing components, Click the 1Channel Plot button. 4. In the plot type, select log. 5. CH 1 is set to This Array, i.e. the array you have selected in the Sample area. If you already have added Spot Image View to the process list, click Open in the Spot Image View� A TextButton component has several important properties. Property displayText is the text to display, action is the function to call when the component is selected (clicked), and disappearOnAction (default is true) handles whether or not you want the button to fade after the function is invoked.

How to Set Image in a Image View on Click in Android Studio, The first Image View sets the image on a button click and the others contain the images to be set. I saved the images in the drawable folder by� Sep 9, 2010. I have a widget and I would like to detect position where user click on some part of the widget's bitmap. I didn't found any way how to detect coordinates of the touch on RemoteViews.Is anybody aware of some trick how to detect it.

ImageView, ImageView is also commonly used to apply tints to an image and Measure the view and its content to determine the measured width and the� Android :: Radio Button Selection Changes Toast; Android :: Spinner - Don't Display Selection / Set Selection Text Color As Transparent? Aug 4, 2009. I'm trying to use Spinner as a button with selection pop-up. Basically I want an icon and when user clicks it - the popup list is displayed and user can make a selection.

ImageButton, The image on the surface of the button is defined either by the android:src attribute in the <ImageButton> XML element or by the ImageView. Android :: Radio Button Selection Changes Toast; Android :: Get User Selection And Convert It To A String; Android :: Spinner Selection Box On Activity And Dialog Jun 22, 2010. I have a spinner selection box on my activity, but now I want to remove the spinner from the main view and make the selection available by an option from the menu button.