Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable TextView doesn't allow to select text after the application is paused

I have a scrollable TextView where a user can select text. I add scroll bar by setting movement method to ScrollingMovementMethod.

Problem: Selection works well unless the application is paused (for instance, after switching apps). Once the app is active again selection stops working and I get the following message in log:

W/TextView: TextView does not support text selection. Selection cancelled.

My setup:

I have an Activity with CoordinatorLayout and a Fragment with a TextView wrapped into RelativeLayout which looks like this:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:scrollbars="vertical" />

And in Java code I have to do:

textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTextIsSelectable(true);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);

because this was the only working way according to this, this and this issues.

EDIT:

The problem is in the following call

textView.setMovementMethod(new ScrollingMovementMethod());

If I remove it it works, but I can't get why.

Minimal steps to reproduce the issue:

1) Create an empty Activity with a TextView using the following layout.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:text="Some very very very long text..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:scrollbars="vertical" />

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

2) Set up visibility parameters of the TextView in onStart() method.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        TextView textView = findViewById(R.id.text_view);
        textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setTextIsSelectable(true);
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
    }
}

3) Try to use context menu on the TextView before and after pausing the application.

EDIT 2:

Removing setMovementMethod(new ScrollingMovementMethod()) solves my problem and the functionality works well after that. But I'm not quite sure why it was added and I'm afraid it will brake something if I remove it. Any idea why one might use ScrollingMovementMethod in combination with android:scrollbars="vertical". May be xml doesn't work in some cases? Ideas? And I'm still interested why using ScrollingMovementMethod brakes selection functionality?

like image 837
Sasha Shpota Avatar asked Oct 11 '18 20:10

Sasha Shpota


1 Answers

I had very similar problem; texview could scroll but not selectable.

Remove this from your code:

texview.setMovementMethod(new ScrollingMovementMethod())

I add these to your textview in xml:

android:scrollHorizontally="true" android:textIsSelectable="true"

like image 59
afakan Avatar answered Oct 22 '22 02:10

afakan