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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With