Rishi
Rishi

Reputation: 3539

How to make an ImageView clickable in a ListView

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

Upvotes: 3

Views: 12384

Answers (7)

kometen
kometen

Reputation: 7792

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" />

Upvotes: 0

Sunil Pandey
Sunil Pandey

Reputation: 7102

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;
    }
}

Upvotes: 4

Will Tate
Will Tate

Reputation: 33509

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
    }
});

Upvotes: 1

user1543537
user1543537

Reputation: 1

Try this for your ImageView. This worked for me.

android:focusable = "false"

Upvotes: 0

Abhijit Srivastava
Abhijit Srivastava

Reputation: 194

To make an ImageView clickable you can set the property

android:clickable="true"

in the xml-file.

Upvotes: 1

marcosbeirigo
marcosbeirigo

Reputation: 11318

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" />

Upvotes: 3

Raul Gonzales
Raul Gonzales

Reputation: 906

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);
    }
});

Upvotes: 0

Related Questions