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

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

        <TextView
            android:id="@+id/solutions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textIsSelectable="true"
            android:textSize="18sp"
            tools:text="1.  1+2+3 = 6"/>
    </HorizontalScrollView>
</ScrollView>

After adding android:textIsSelectable="true" to the TextView, the TextView will auto-scroll to bottom. However I do not want this behavior.

2 Answers 2

0

For people with the same problem: You have two Scrollviews, you might need to play around a bit or use NestedScrollView. In the end this worked for me:

android:descendantFocusability="afterDescendants

on the parent of my TextView

Try this:

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

    <HorizontalScrollView
       android:descendantFocusability="afterDescendants"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/solutions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textIsSelectable="true"
            android:textSize="18sp"
            tools:text="1.  1+2+3 = 6"/>
    </HorizontalScrollView> </ScrollView>

UPDATE: Although this seemed to work, it didn't. The view just jumped randomly so in my tests it looked fine at first.

I can't find a proper solution so I'm using dirty hacks. My textview is now "nonselectable". In my onCreateView() of my Fragment, I wait 1,5 seconds in a new thread and update the textview from the UI thread AFTER the Fragment view is fully build. That way the textview is not re-focused and the scrollview doesn't jump around:

        Runnable x = () -> {
            try {
                Thread.sleep(1500);
                if(getActivity()!=null){

                    getActivity().runOnUiThread(() -> {
                        textview.setFocusable(true);
                       textview.setFocusableInTouchMode(true);
                        textview.setTextIsSelectable(true);
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        new Thread(x).start();
-1

try this: add

android:focusable="false"
android:focusableInTouchMode="false"

 <TextView
        android:id="@+id/solutions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:lineSpacingMultiplier="1.2"
        android:text="1.  1+2+3 = 6"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textIsSelectable="true"
        android:textSize="18sp" />

The reason of the solution is :

When you use android:textIsSelectable="true", you're essentially calling TextView.setTextIsSelectable(true), which makes calls to both View.setFocusable and View.setFocusableInTouchMode....and causes the scrollview to automatically scroll the text.

5
  • 4
    That makes the text unselectable
    – Andre
    Mar 13, 2017 at 13:46
  • Pleas tell us more about your idea.
    – Felix D.
    Apr 18, 2017 at 15:08
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add an explanation, and give an indication of what limitations and assumptions apply.
    – Dev-iL
    Apr 18, 2017 at 15:20
  • 4
    I confirm this makes the text unselectable on API 23, so it's useless.
    – mhsmith
    Feb 4, 2018 at 13:03
  • Result Logcat: TextView W TextView does not support text selection. Selection cancelled. Nov 1, 2022 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.