0

I have a textview which text must be selectable textView.setTextIsSelectable(true) and scrollable to bottom by default textView.setMovementMethod(ScrollingMovementMethod.getInstance()).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:background="@color/colorScreenBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xxxx.xxxx.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical"
    android:weightSum="100">

    <TextView
        android:id="@+id/result"
        android:scrollbars = "vertical"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:elevation="8dp"
        android:textIsSelectable="true"
        android:gravity="bottom"
        android:background="@color/white"
        android:padding="5dp"
        android:focusable="true"
        android:fastScrollEnabled="true"
        android:fastScrollAlwaysVisible="true"
        android:scrollbarSize="7dip"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60"
        android:background="@color/gray_background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
            tools:listitem="@layout/recyclerview_item"
            android:background="@color/gray_background"
            />
    </androidx.core.widget.NestedScrollView>
</LinearLayout>

The problem is that the setMovementMethod disable the textview selectability.

How to solve this?

NB: The principal feature required here is that not only the textview must be scrollable, but it must also appeared initially scrolled to bottom by default, and this is provided by the setMovementMethod(ScrollingMovementMethod.getInstance()), according to How to get the Android TextView to scroll down to the end automatically.

Mahouk
  • 902
  • 9
  • 28

1 Answers1

1

Put your text view inside an scroll view in your xml

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">
    <TextView
        android:id="@+id/textView"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        android:textIsSelectable="true"
        android:gravity="bottom"
        android:background="@color/white"
        android:padding="5dp"
        android:text="@string/dummy"
        android:focusable="true"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
    </ScrollView>

Then do this in your class

//This will scroll to bottom 
ScrollView scrollView=findViewById(R.id.scrollView);
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
Shamir Kp
  • 488
  • 5
  • 9