Textview AutoSizing of RecyclerView in Android

977 views Asked by At

I've had weird experience when i scroll the RecyclerView. There is a item_layout for the RecyclerView's item. The item layout xml consists of 3 Textview and 1 ConstraintLayout. the rootview is ConstriantLayout.

please check this image. enter image description here

All the TextView's layout_height is wrap_content and layout_width is 0dp(match_constraint) but basically TextView autosize doesn't work correctly when the view's layout_height is wrap_content. so everytime i scroll the RecyclerView, the TextView3's characters become smaller and then get back to the maxSize. so i changed the layout_height of TextView3 into 0dp(match_constrarint). it worked fine. but the reason why i ask is TextView1 and TextView2 works fine even though they still have wrap_content. how does it work???

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:maxLines="1"
    android:textSize="@dimen/detail_txt_title"
    app:autoSizeMaxTextSize="@dimen/detail_txt_title"
    app:autoSizeMinTextSize="@dimen/detail_txt_summary"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toTopOf="@id/TextView2"
    app:layout_constraintRight_toLeftOf="@id/TextView3"
    app:layout_constraintHorizontal_weight="2.1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:maxLines="1"
    android:textColor="@color/black"
    android:textSize="@dimen/detail_txt_title_small"
    app:autoSizeMaxTextSize="@dimen/detail_txt_title_small"
    app:autoSizeMinTextSize="@dimen/detail_txt_summary"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="@id/textView1"
    app:layout_constraintLeft_toLeftOf="@id/textView1"
    app:layout_constraintTop_toBottomOf="@id/textView1" />

 <androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/textview3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:gravity="center"
    android:maxLines="1"
    android:textColor="@color/daily_history_text_black"
    android:textSize="@dimen/detail_txt_title"
    app:autoSizeMaxTextSize="@dimen/detail_txt_title"
    app:autoSizeMinTextSize="@dimen/detail_txt_summary"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_weight="2.1"
    app:layout_constraintLeft_toRightOf="@id/textview1"
    app:layout_constraintTop_toTopOf="parent" />
2

There are 2 answers

0
avalerio On BEST ANSWER

See this: Autosize textview is not working It seems to be normal for autoSize text to produce inconsistent results when used with wrap_content the fact that it is working is just one of those inconsistences.

0
Mohammed Mujalli On

Your mistake was only in the names of the textviews and here is the fix for your question:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
   >

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toTopOf="@id/textView2"
        app:layout_constraintRight_toLeftOf="@id/textview3"
        app:layout_constraintHorizontal_weight="2.1"
        android:text="textView1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:textColor="@color/black"
        app:autoSizeTextType="uniform"
        android:text="textView2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="@id/textView1"
        app:layout_constraintLeft_toLeftOf="@id/textView1"
        app:layout_constraintTop_toBottomOf="@id/textView1" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textview3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:text="textView3"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="2.1"
        app:layout_constraintLeft_toRightOf="@id/textView1"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

or you can use this way:

<?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="wrap_content"
    tools:context=".MainActivity"
  >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center">
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            app:autoSizeTextType="uniform"
            app:layout_constraintBottom_toTopOf="@id/textView2"
            app:layout_constraintRight_toLeftOf="@id/textview3"
            app:layout_constraintHorizontal_weight="2.1"
            android:text="textView1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            android:textColor="@color/black"
            app:autoSizeTextType="uniform"
            android:text="textView2"
            />

    </LinearLayout>
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textview3"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:text="textView3"
        app:autoSizeTextType="uniform"
      />

</LinearLayout>