Android TextView does not update its text if textIsSelectable is set to true

I ran into this strange problem that caused me a lot of headache and trouble.

I published an application that is a repository of several books, I decided to use TextView to display the text of each chapter, because it meets the requirements and is quick and easy to use.

The user asked me to select the text and copy it, I used textIsSelectable = true, and everything was fine, the next day he called me to inform that the text does not change, you open a chapter and switch to another chapter but the text remains that same.

I thought it might be some kind of logical problem on my part, but after some debugging the problem was in the TextView when I set textIsSelectable = false; all was good.

To make sure I used a static int that increments every time the text is displayed, the text remains unchanged.

So what is the problem? am i using textview incorrectly? or is this some kind of error in the textview itself?

Here's my layout (txtBody is the TextView that causes the problem)

        <?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/white">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:fillViewport="true"
            >

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical"
                android:gravity="right">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title Text"
                    android:id="@+id/txtTitle"
                    android:textSize="24sp"
                    android:padding="5dp"
                    android:gravity="right"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Detail Text"
                    android:id="@+id/txtBody"
                    android:background="@android:color/white"
                    android:gravity="right"
                    android:textColor="@android:color/black"
                    android:textSize="16sp"
                    android:padding="5dp"
                    android:enabled="true"
                    android:textIsSelectable="true"
                    android:selectAllOnFocus="false"
                    android:soundEffectsEnabled="true"/>

        </LinearLayout>


    </ScrollView>


</FrameLayout>

I use this code to set the text:

String body = MainDataManager.getInstance().getChapterBody(book.getTable(), chapter.getId());
    updateTextStyle();
    txtTitle.setText(chapter.getTitle());
    txtBody.setText(body, TextView.BufferType.SPANNABLE);
+5
source share
1 answer

Uase invalidate () before changing the text of the text field.

 String body = MainDataManager.getInstance().getChapterBody(book.getTable(), chapter.getId());
    updateTextStyle();
    txtTitle.invalidate();
    txtTitle.setText(chapter.getTitle());
    txtBody.invalidate();
    txtBody.setText(body, TextView.BufferType.SPANNABLE);
0
source

All Articles