1

I am trying to select text from a TextView which is present inside a fragment. This is the XML for my TextView. The textIsSelectable, focusable, enabled and longclickable properties are all set to true according to this post.

                <TextView
                    android:id="@+id/post_text_recycle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/post_divider"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:autoLink="web"
                    android:fontFamily="serif"
                    android:text="@string/stall_user"
                    android:textColor="@android:color/black"
                    android:textColorHighlight="@android:color/holo_blue_light"
                    android:textIsSelectable="true"
                    android:focusable="true"
                    android:enabled="true"
                    android:longClickable="true"
                    android:textSize="16sp" />

I have also programatically set the following in my Fragment activity:

text.setTextIsSelectable(true);

If it helps, I am fetching data from a Bundle passed through the Activity that holds the fragment, then I am setting the TextView using:

text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);

I am still unable to select the text. I read in some Stack Overflow post that setting width/height to "wrap_content" allows you to select the text (some old Android bug I guess). This trick has worked for my recyclerview TextView in another activity. Doesn't seem to work here.

Thanks for your help!

Edit: Here is the entire Fragment layout code as requested:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.www.ViewFragments.OriginalPostFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/hide_toggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:text=""
                android:layout_alignParentTop="true"
                android:background="#eeeeee"
                android:gravity="end"
                android:padding="4dp"
                android:layout_marginEnd="16dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:elevation="6dp"
                />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/detail_wrapper"
                android:background="#ffffff"
                android:descendantFocusability="blocksDescendants"
                android:paddingBottom="48dp"
                cardview:cardCornerRadius="4dp"
                cardview:cardElevation="4dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/user_image_recycle"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp" />

                    <TextView
                        android:id="@+id/post_type_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:layout_toRightOf="@+id/user_image_recycle"
                        android:textStyle="italic" />

                    <TextView
                        android:id="@+id/post_title_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/post_type_recycle"
                        android:layout_marginStart="16dp"
                        android:layout_toRightOf="@+id/user_image_recycle"
                        android:maxLines="1"
                        android:textColor="@android:color/black"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/user_name_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/post_title_recycle"
                        android:layout_marginStart="16dp"
                        android:layout_toEndOf="@+id/user_image_recycle"
                        android:textColor="#0094BD"
                        android:textSize="16sp" />

                    <ImageView
                        android:id="@+id/post_divider"
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_below="@+id/user_image_recycle"
                        android:layout_marginEnd="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:background="@android:color/black" />

                    <TextView
                        android:id="@+id/post_text_recycle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/post_divider"
                        android:layout_marginEnd="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:autoLink="web"
                        android:fontFamily="serif"
                        android:text="@string/stall_user"
                        android:textColor="@android:color/black"
                        android:textColorHighlight="@android:color/holo_blue_light"
                        android:textIsSelectable="true"
                        android:focusable="true"
                        android:enabled="true"
                        android:longClickable="true"
                        android:textSize="16sp" />

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

        </RelativeLayout>
    </ScrollView>

</FrameLayout>
Saunved Mutalik
  • 381
  • 2
  • 19

1 Answers1

0

The reason is descendantFocusability. In Your CardView layout, you set descendantFocusability to blocksDescendants. What it does it disable the focusability of the descendants, so Your TextView never get those focus events. For more info see here.

To make it work just remove this line from CardView

android:descendantFocusability="blocksDescendants"
theJango
  • 1,100
  • 10
  • 22
  • Thank you. It worked. I had blocked descendants in another place because the view would scroll down automatically. It's not doing that here, so I guess it should be fine if I remove it. Thanks again! – Saunved Mutalik Jul 23 '18 at 14:50
  • Hi, i have the same problem but havent got any android:descendantFocusability="blocksDescendants" in my xml. Keep can't understant why I can't select – NeoKerd Jan 21 '20 at 14:24