3

I have created a basic ListView and added a TextView and ImageView in it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
        <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
    />
        

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50px"
        android:paddingLeft="2px"
        android:paddingRight="2px"
        android:paddingTop="2px"
        android:layout_height="wrap_content"
        android:layout_alignParentRight = "true"
        android:src="@drawable/call"
    />

</RelativeLayout>

I need to make ImageView clickable so that if some clicks on it an action will happen, like a new activity is opened. Can someone help me how to do this?

Thanks

kometen
  • 6,536
  • 6
  • 41
  • 51
Rishi
  • 3,499
  • 8
  • 35
  • 54

7 Answers7

4

You have to implement your own cursor adapter, and in that you have to override the getView method and then set the onclick listener to your image:

public class SMSimpleCursorAdapter extends SimpleCursorAdapter {

    Context context;
    Activity activity;
    public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.activity = (Activity) context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        long id = getItemId(position);
        ImageView image = (ImageView)view.findViewById(R.id.icon);
        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Perform action
            }
        });

        return view;
    }
}
kometen
  • 6,536
  • 6
  • 41
  • 51
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
3

Use ImageButton instead:

<ImageButton
    android:id="@+id/icon"
    android:layout_width="50px"
    android:paddingLeft="2px"
    android:paddingRight="2px"
    android:paddingTop="2px"
    android:layout_height="wrap_content"
    android:layout_alignParentRight = "true"
    android:background="@drawable/call" />
kometen
  • 6,536
  • 6
  • 41
  • 51
marcosbeirigo
  • 11,098
  • 6
  • 39
  • 57
1

To make an ImageView clickable you can set the property

android:clickable="true"

in the xml-file.

kometen
  • 6,536
  • 6
  • 41
  • 51
  • This option seems the best and simpliest option, no? Small addition: you also need to add in your Activity what Will Tate and Raul Gonzales wrote. – Boommeister Dec 05 '20 at 08:40
1

Try something like the following:

ImageView myImg = (ImageView) findViewById(R.id.icon);
myImg.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Launch Intent or whatever you want here
    }
});
kometen
  • 6,536
  • 6
  • 41
  • 51
Will Tate
  • 33,439
  • 9
  • 77
  • 71
0

Try this for your ImageView. This worked for me.

android:focusable = "false"
kometen
  • 6,536
  • 6
  • 41
  • 51
  • 1
    Hey welcome to SO! could you expand on your answer a bit? that would definitely help other people understand how to do this better. – Jeff Tratner Jul 22 '12 at 05:33
0

this works:

ImageView myImg = (ImageView) findViewById(R.id.icon);
myImg.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(YourCurrentActivity.this,YourNextActivity.class);
            startActivity(i);
    }
});
kometen
  • 6,536
  • 6
  • 41
  • 51
Raul Gonzales
  • 866
  • 1
  • 15
  • 28
0

Late to the game but this thread was very helpful.

This is how I made an ImageView clickable inside a ListView. The OnClickListener() is added to the delete-icon for each item using View. Only issue with this solution is I need to press the delete-icon twice before it gets focus.

public class PurchaseFragment extends Fragment  {

    private final ArrayList<Product> products = new ArrayList<>();

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

        products.add(product);

        productListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast listToast = Toast.makeText(getActivity(), "product: " + adapterView.getItemAtPosition(i), Toast.LENGTH_SHORT);
                listToast.show();

                ImageView deleteIcon = (ImageView) view.findViewById(R.id.delete_icon);
                deleteIcon.setClickable(true);
                deleteIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast itemToast = Toast.makeText(getActivity(), "remove " + adapterView.getItemAtPosition(i), Toast.LENGTH_SHORT);
                        itemToast.show();
                    }
                });
            }
        });
        productListview.setAdapter(adapter);
    }
}
<TextView
    android:id="@+id/product_textview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@android:drawable/editbox_background"
    android:padding="8dp"
    android:textColor="@color/black"
    android:textSize="16sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/delete_icon"
    android:layout_width="41dp"
    android:layout_height="40dp"
    android:background="@null"
    android:padding="8dp"
    android:src="@drawable/stop_sign_white_vertical_line"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.97"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="0dp" />
kometen
  • 6,536
  • 6
  • 41
  • 51