Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android textIsSelectable stops TextView onClick working correctly

In my current Android application I have a MaterialCardView that wraps a TextView that I wish to be copyable e.g. I use

android:textIsSelectable="true"

However, I also need to know when the MaterialCardView is clicked as its within a RecyclerView Item layout.

My onClickListener for the MaterialCardView is only triggered when I remove the android:textIsSelectable="true" attribute from the child TextView.

here is the complete RecyclerView Item layout

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_bibtext_cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    card_view:cardBackgroundColor="?attr/colorSurface"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp">

    <TextView
        android:id="@+id/bibtex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textIsSelectable="true"
        android:background="?android:attr/selectableItemBackground"
        android:textColor="?android:attr/textColorPrimary" />

</com.google.android.material.card.MaterialCardView>

How can I both detect clicks on the Parent MaterialCardView and have the child TextView selectable?

like image 378
Hector Avatar asked Sep 02 '26 13:09

Hector


1 Answers

In your case (with textIsSelectable="true") the TextView will still trigger an OnClick event. So you could simply add an OnClickListener to your TextView and link that to the existing CardView click behavior.

Would that cover your case?

like image 179
Daan Avatar answered Sep 05 '26 12:09

Daan