AppCompatTextView autoSizeTextType not working

3.7k views Asked by At

In my activity.xml, I have two AppCompatTextView that looks like this:

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/user_full_name_text_view"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="8dp"
    android:fontFamily="@font/roboto_bold"
    android:maxLines="1"
    android:text="@string/full_name"
    android:textColor="@android:color/white"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/profile_image"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/user_name_text_view"
    android:layout_width="0dp"
    android:layout_height="20dp"
    android:layout_marginEnd="8dp"
    android:fontFamily="@font/roboto"
    android:maxLines="1"
    android:text="@string/username"
    android:textColor="@android:color/white"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toEndOf="@+id/user_view"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/user_full_name_text_view"
    app:layout_constraintTop_toBottomOf="@+id/user_full_name_text_view"
    app:layout_constraintVertical_bias="0.0" />

In my design tab, this part of the activity looks like this:

Android Design Tab

Now that looks correct as the text has correctly auto sized to fill the height of the first AppCompatTextView. However, when I run my app on mobile, it looks like this:

Android Mobile Design

Why is the text not auto sizing? How do I fix this so that way the text fills the entire width and height of the AppCompatTextView?

2

There are 2 answers

0
cactustictacs On

Auto sizing doesn't play well with wrap_content, probably because it needs to know its size before it can say how big its content will be. Do you have any of that going on anywhere in the hierarchy, like in the layout your AppCompatTextViews are in?

Right now your widths depend on how wide parent is, but if that's wrap_content (or its parent is, etc etc) then eventually that text view is gonna get asked how wide it wants to be, and it gets confused

1
aminography On

I guess the problem is that adding app:autoSizeTextType="uniform" is not enough. When you are using uniform type, you should specify step granularity as well. Try to use such attributes below:

<androidx.appcompat.widget.AppCompatTextView
    ...
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeMaxTextSize="24sp"
    app:autoSizeStepGranularity="1sp"
    ... />