12

This is the error I get: W/TextView: TextView does not support text selection. Selection cancelled.

I am stunned, because other Textviews I have implemented before in a RelativeLayout have had text selectable when I have the property android:textIsSelectable="true" - however, doesn't work this time.

This code is placed under app_bar_main.xml using an include keyword in XML.

Here is app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            >
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" /> //This is where the textview is added!

</android.support.design.widget.CoordinatorLayout>

Here is the edited content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context="com.androidterminal.MainActivity"
    tools:showIn="@layout/app_bar_main">
    //I have some other LinearLayouts here
 <RelativeLayout
        android:id="@+id/ll5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll2"
        android:orientation="horizontal">
    <TextView
        android:id="@+id/main1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGrey"
        android:textIsSelectable="true"
        android:textColor="#000000" />
    </RelativeLayout>
</RelativeLayout>

This TextView main1 is not selectable and throws the aforementioned warning on LogCat. Please help. Thanks. (I have already tried setting focusable and setFocusable properties)

Onik
  • 19,396
  • 14
  • 68
  • 91
Zac
  • 695
  • 1
  • 11
  • 34

4 Answers4

7

Your code works fine, I am not sure what config you have on your system.

Try the following fix

  1. Try this in java.

    textview.setTextIsSelectable(true);
    textview.setFocusable(true);
    textview.setFocusableInTouchMode(true);
    
  2. Change TextView width to wrap_content from match_parent.

Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
44kksharma
  • 2,740
  • 27
  • 32
2

Using following CopyTextView.java can resolve your problem:

public class CopyTextView extends EditText {

private boolean mEnabled; // is this edittext enabled

public CopyTextView(Context context) {
    super(context);
}

public CopyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CopyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        if (!mEnabled) return;
        super.setEnabled(false);
        super.setEnabled(mEnabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void setEnabled(boolean enabled) {
    this.mEnabled = enabled;
    super.setEnabled(enabled);
}}             

and then use it in your layout file

 <com.your_package_name.CopyTextView
        android:id="@+id/main1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGrey"
        android:textIsSelectable="true"
        android:textColor="#000000" />
Digvijay Singh
  • 623
  • 9
  • 26
1

Look at this,It works , i have removed your styles which you have given in your xml

enter image description here

jeel raja
  • 667
  • 5
  • 11
0

logically thinking, the text can be selected by long click.... so textview has to be longclickable

<TextView
    android:id="@+id/main1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorGrey"
    android:textIsSelectable="true"
    android:focusable="true"
    android:longClickable="true"
    android:textColor="#000000" />