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