Autosizing of TextView doesn't work (Android O)

Viewed 37293

I use new autosize feature added in support library 26. I read a documentation which can be found here : https://developer.android.com/preview/features/autosizing-textview.html

I suppose that it should work this way: You can enable auto-sizing with this attribute: app:autoSizeTextType="uniform". I think that TextView should use all available space to display a whole text (not just a part - it shouldn't be cropped) and the textSize should be as big as possible. If you need to limit a maximum or minimum size of the text then you can use these two attributes:

app:autoSizeMinTextSize="XXsp" // (you can also use px or dp values.)

or

app:autoSizeMaxTextSize="XXsp"

So far so good. Let's say that I need a TextView with 56dp width. I have texts with a different length and I want to set these texts to this TextView. It should be automatically resized so it displays the whole text (all characters + not cropped) in the biggest possible textSize.

This is my TextView:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/vName"
        style="@style/TextView.AutoSize"
        android:layout_width="56dp"
        android:gravity="bottom|center_horizontal"
        android:maxLines="1"
        app:autoSizeMinTextSize="1px"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toTopOf="@id/vGuideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

Unfortunately, the text view is cropped. I wanted to set this String as a text to the TextView above: "Groupa" but this is the result: enter image description here

(TextView is inside ConstraintLayout with yellow circle background.)

As you can see the textview is not resized at all. Do you have any idea what to do?

Thanks.

12 Answers

Additional info to @Sira Lam 's accepted answer:

Always make sure you don't inherit attributes that might conflict AutoTextSize's behavior, particularly android:singleLine.

Even though android:maxLines or android:lines do not obstruct the TextView from sizing accordingly, the singleLine attribute (when set to true) completely disables any auto sizing.

So, when tracking down an AutoTextSize issue, first try searching for the singleLine attribute, as it is sometimes inherited when extending Button.

Make sure to check these options in order to autoSize work:

  • Don't use wrap_content in any dimensions (such as layout_width and layout_height)
  • Make sure to not use singleLine, instead use android:maxLines
  • Use app:autosizexxx instead of android:autosizexxx in order to support older android versions
  • Autosize works in both AppCompatTextView and TextView, so you don't need to change your View

if you do not know size of textview. e.g. you put them in linearlayout and set height or width to 0dp. then I got a solution. you need to setAutoSizeTextTypeWithDefaults in OnSizeChanged event.

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);

    }

Makesure use android:maxLines=1 as @Henning said.

<androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"

            android:maxLines="1"
            app:autoSizeMaxTextSize="16sp"
            app:autoSizeMinTextSize="12sp"
            app:autoSizeTextType="uniform"

            tools:text="Downloading" />

Beside autosize attributes for single line TextView I had to add textSize too, so the height which was wrap, was big enough to let the text resize to it's maximum size defined

android:lines="1"
android:maxLines="1"
android:textSize="30sp"
app:autoSizeMaxTextSize="30sp"
app:autoSizeMinTextSize="12sp"
app:autoSizeTextType="uniform"
Related