Autosizing of TextView doesn't work (Android O)

喜你入骨 提交于 2019-11-28 18:35:22

I have tested this for a few situations, and have the below conclusion:

You must have bounded width and height. For example, if you set width to be match_parent but wrap_content for height, I think Android doesn't know that how high you want to stretch your text. In your example you don't have a specific height, so I think that's why it doesn't work.

For example:

I don't know why Android official document would use wrap_content as an example...

And as you can see I didn't use other attributes in my example, so it probably is not the problem of incorrect attributes.

And, yes, the TextView I am using is android.support.v7.widget.AppCompatTextView.

And as long as you are using support library 26.0.0 or above it is good enough.

EDIT:

As for ConstraintLayout, the principal is the same. You should have both bounded width and height, which means either one of below for each dimension:

  1. You have specified an absolute value for that dimension (width or height)

  2. You have set Constraint to both directions

For example:

UPDATE: (2017-09-21)

I have tested that unfortunately it seems it does not support custom typeface yet, which is a function published together in support library v26...

Additional to the other correct answers I found another point which prevents autosizing to work.

Do not use android:singleLine="true" together with autosizing. Use the newer android:maxLines="1" instead.

Apollo

I had the same issue. I solved it by changing two lines in my gradle: compile 'com.android.support:support-v4:26.0.1' and compile 'com.android.support:appcompat-v7:26.0.1' To fit longer texts you have to add all four options, like this:

<android.support.v7.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/your_string"
        app:autoSizeTextType="uniform"
        app:autoSizeMaxTextSize="13sp"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeStepGranularity="1sp"/>

Which value did you set to android:layout_height attribute ?

From the document: "If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results."

In my case something very stupid was the issue: While autosizing always worked fine for me, on exactly one TextView I used the android: namespace instead of app:! I was totally oblivious to my mistake and kept wondering why it didn't work. So when using an AppCompat theme always make sure to use the AppCompat attributes, not the native ones.

Config your TextView like this

<TextView
    android:id="@+id/vName"
    android:layout_width="56dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="Groupa"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeMaxTextSize="20sp"
    app:autoSizeTextType="uniform"
    />

Work well on Android 22, 23, 26

Have you tried setting all of the four attributes described in the link you posted?

E.g.

app:autoSizeTextType="uniform"
app:autoSizeMaxTextSize="13sp"
app:autoSizeMinTextSize="5sp"
app:autoSizeStepGranularity="1sp"

You can also try setting both width and height to wrap_content, and setting minWidth and maxWidth to 56dp.

As poss also mentioned in the comments, maxlines seems to cause problems (for me as well), so try removing that (The autosizing should probably take care of this, by reducing the textsize).

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);

    }

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.

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" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!