Onclick doesn't get triggered on CardView
Asked Answered
S

3

9

I have an OnClickListener on a CardView. The listener only works when I tap on a region outside the content(TextViews/ImageViews). I also have a linear layout inside my CardView. I wanted it to work when I tap anywhere on the CardView. Is there a better way to implement this compared to how I'm doing it?

Here's my CardView in xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:padding="10dp"
    android:layout_margin="3dp"
    android:elevation="3dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/post_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/guilt_theme"
            android:padding="4dp"
            android:text="GUILT"
            android:textColor="@color/textColor"
            android:textSize="12sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/post_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:layout_marginTop="10dp"
            android:focusableInTouchMode="false"
            android:clickable="true"
            android:text="text content"
            android:textColor="@color/secondaryText"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/date_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:inputType="number"
            android:text="on 16/5/2018 by user"
            android:textColor="@color/weaker_text"
            android:textSize="14sp" />

        <LinearLayout
            android:id="@+id/LinearLayout"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/view_image"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:clickable="true"
                android:focusable="true"
                android:background="?attr/selectableItemBackground"
                android:layout_marginTop="4dp"
                android:src="@drawable/ic_action_name" />

            <TextView
                android:id="@+id/total_views"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:inputType="number"
                android:layout_marginTop="3dp"
                android:textColor="@color/secondaryText"
                android:textSize="14sp"
                android:textStyle="bold" />
            <ImageView
                android:layout_marginLeft="20dp"
                android:id="@+id/like_image"
                android:layout_width="20dp"
                android:layout_marginTop="4dp"
                android:clickable="true"
                android:focusable="true"
                android:background="?attr/selectableItemBackground"
                android:src="@drawable/thumbs_up"
                android:layout_height="20dp" />

            <TextView
                android:id="@+id/total_upvotes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:layout_marginTop="3dp"
                android:inputType="number"
                android:textColor="@color/secondaryText"
                android:textSize="14sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/dislike_image"
                android:layout_marginLeft="20dp"
                android:layout_width="20dp"
                android:layout_marginTop="4dp"
                android:src="@drawable/thumb_down"
                android:clickable="true"
                android:focusable="true"
                android:background="?attr/selectableItemBackground"
                android:layout_height="20dp" />

            <TextView
                android:id="@+id/total_downvotes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:layout_marginTop="3dp"
                android:inputType="number"
                android:textColor="@color/secondaryText"
                android:textSize="14sp"
                android:textStyle="bold" />



        </LinearLayout>
    </LinearLayout>
 </android.support.v7.widget.CardView>

I then added a listener to it in my Activity.

 viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent = new Intent(getActivity(), PostReader.class);
               TextView posttext = view.findViewById(R.id.post_text);
               String post = posttext.getText().toString();
                intent.putExtra("postinfo", post);
                startActivity(intent);
            }
        });
Softener answered 11/7, 2018 at 17:34 Comment(0)
S
9

I think you are getting that behaviour because some of your TextViews and ImageViews have the attribute android:clickable="true"

Try:

Remove the attributes and add it only to the CardView. You may also have to remove the attribute android:focusable="true" from the ImageViews so that the cardview can listen to click events.

Spoony answered 11/7, 2018 at 17:50 Comment(1)
In my case I had inner vertical layout, that was robbing clicks.Abb
B
2

In my case the issue was setting the inner view of the CardView to catch clicks by setting android:clickable="true" & android:focusable="true" to the inner view. This prevents the CardView from catching clicks.

Removing both allowed the CardView to catch clicks.

Bissextile answered 17/6, 2022 at 19:7 Comment(0)
Z
0

I've facing a problem with it today, a card view with the view I created was not performing the click function.
I tried many things and nothing worked. My solution was only to take the content of the card view and insert it into a constraint layout requiring all the view area. After that the click started working normally.
Example

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:backgroundTintMode="add"
    android:elevation="5dp"
    app:cardBackgroundColor="@android:color/transparent"
    app:cardCornerRadius="5dp"
    app:strokeColor="@color/yellow"
    app:strokeWidth="2dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/textMove"
            style="@style/TextAppearance.MaterialComponents.Subtitle2"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:padding="2dp"
            android:text="@string/move"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Zymometer answered 7/12, 2022 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.