Android TextView autosizing takes more space than regular TextView

405 views Asked by At

I have this simple piece of code where I want the TextView Contents to be resized. On initial impression, the Text inside the TextView has the content shrunk, which is great, but upon enabling layout bounds I see that TextView with AutoSizing actually is taking more space on the screen than a TextView that does not for the same content.

Here is my code:

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

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:text="Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello"
        app:autoSizeMaxTextSize="12sp"
        app:autoSizeMinTextSize="4sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_2"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/tv_2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:ellipsize="end"
        android:text="Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv_1"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot

Am I missing something? Any input will be greatly appreciated.

1

There are 1 answers

1
paulfranco On

With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries.

Default setting lets the autosizing of TextView scale uniformly on horizontal and vertical axes.

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview