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);
}
});
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.
Navigate to the app > java > your apps package name > Right Click on it > New > Java Class and name your Adapter Class(Here CourseAdapter). Adapter Class in RecyclerView will get the data from your Modal Class and set that data to your item of RecyclerView.
CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With