Copy function in expandableTextView generates an error when changing orientation

I am creating some extensible TextView in a fragment without the ability to copy. And it works great even if I change the orientation.

But when I gave android:textIsSelectable="true"in xml a TextView, the problem I came across was:

  • When the orientation was changed, all TextView content was changed with the contents of the last TextView. Can I find out why this is happening and what can I do to fix it?

fragment_layout.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dddddd">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/sample1"
                layout="@layout/text_item" />

            <include
                android:id="@+id/sample2"
                layout="@layout/text_item" />

            <include
                android:id="@+id/sample3"
                layout="@layout/text_item" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

text_item.xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="bottom"
        android:singleLine="true"
        android:textColor="#333333"
        android:textSize="18sp"
        android:padding="5dp"
        android:background="@color/title_bg_color"

        />

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expand_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        expandableTextView:maxCollapsedLines="2">

        <TextView
            android:id="@id/expandable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:fontFamily="sans-serif-light"
            android:textSize="16sp"
            android:textColor="#494848"
            android:padding="5dp"
            android:background="@color/text_bg_color"
            android:textIsSelectable="true"
            />
        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:padding="16dp"
            android:layout_gravity="right|bottom"
            android:background="@color/text_bg_color"
            /> <!--android:background="@android:color/transparent"-->
    </com.ms.square.android.expandabletextview.ExpandableTextView>

</LinearLayout>

activity.java:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    ((TextView) rootView.findViewById(R.id.sample1).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> A</font>"));
    ((TextView) rootView.findViewById(R.id.sample2).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> B</font>"));
    ((TextView) rootView.findViewById(R.id.sample3).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> C</font>"));

    ExpandableTextView expTv1 = (ExpandableTextView) rootView.findViewById(R.id.sample1)
            .findViewById(R.id.expand_text_view);

    ExpandableTextView expTv2 = (ExpandableTextView) rootView.findViewById(R.id.sample2)
            .findViewById(R.id.expand_text_view);

    ExpandableTextView expTv3 = (ExpandableTextView) rootView.findViewById(R.id.sample3)
            .findViewById(R.id.expand_text_view);

    expTv1.setText(Html.fromHtml(getString(R.string.A)));
    expTv2.setText(Html.fromHtml(getString(R.string.B)));
    expTv3.setText(Html.fromHtml(getString(R.string.C)));

    return rootView;
}

Screenshots when copying is enabled: Content is changed in the landscape and content is changed in the portrait

+4

Source: https://habr.com/ru/post/1685383/


All Articles