Autosizing of TextView doesn't work (Android O)
Asked Answered
E

12

55

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.

Epicardium answered 22/5, 2017 at 16:35 Comment(10)
what is your target API level?Drove
my target API level is 26.Epicardium
Just tried it and have similar problem, i see that text shrinks a a little bit but not enough to fit the whole text. I'm using support 26Ionopause
I was able to get it working by limiting text sizes and granularity, which generates a smaller array <TextView android:id="@android:id/title" style="@style/TextAppearance.AppCompat.Title" android:maxLines="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/primary_text_dark" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="12sp" app:autoSizeMaxTextSize="20sp" app:autoSizeStepGranularity="2sp" tools:text="Title" />Ionopause
Thanks, I will definitely try it.Epicardium
The documentation says android.support.v4.widget package, but you are using v7. However, I can't find it in v4. I think documentation is wrong.Lawry
for me this problem seems to be caused by maxlinesMouseear
Did you put it inside a LinearLayout? (As per Android's Autosizing TextViews guide).Hartung
just go through this link. it may help you . code.tutsplus.com/tutorials/…Hydrogenolysis
I had the same problem when TextView was in the ScrollView (ScrollView child can not has both width and heigh not wrap_content, but autosizing doesn't work with wrap_content)Statolatry
N
57

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:

wrap content height for a text

specific height for a text

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:

All 4 directions are constrained Height set to be an absolute value Width set to be an absolute value

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...

Nonagon answered 18/8, 2017 at 17:52 Comment(7)
@Hartung I have added example for ConstraintLayout. If you have a case that doesn't work, I would like to know tooNonagon
Is there any way to make this work for android EditText as well?Unamerican
@Unamerican EditText extends TextView so it should works, but I haven't tried though. If it does not work, I think there is no easy way thenNonagon
I had previously made a custom fonted textview, which extends the AppCompatTextView. That's working great with the autosize features. So if you need custom fonts with autosize, you can try it out. (similar to what's found here, but extending the AppCompatTextView instead of just normal TextViewDisorient
@JustinLiu Thanks! By the way, be careful of subclassing textview for custom font, caching of typeface need to be done or it will eat up a lot of system resources (createTypeFace is a heavy method and it will be called everytime when textview is created, if you don't cache it)Nonagon
@SiraLam Thanks for confirming that the new autosizing TextView features do not work with custom typefaces. I've been facing an issue getting the feature to work with a custom typeface and this is the only other resource I've found to confirm the issue.Cp
@SiraLam not sure if you'd still like to investigate this. However, from my experience, I haven't been able to autosizing to work with EditText. I have my EditText bounded in both width and height. I've tried to define autosizing both in XML and programatically and it doesn't appear to work.Oppress
B
59

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.

Broke answered 12/2, 2018 at 13:38 Comment(0)
N
57

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:

wrap content height for a text

specific height for a text

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:

All 4 directions are constrained Height set to be an absolute value Width set to be an absolute value

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...

Nonagon answered 18/8, 2017 at 17:52 Comment(7)
@Hartung I have added example for ConstraintLayout. If you have a case that doesn't work, I would like to know tooNonagon
Is there any way to make this work for android EditText as well?Unamerican
@Unamerican EditText extends TextView so it should works, but I haven't tried though. If it does not work, I think there is no easy way thenNonagon
I had previously made a custom fonted textview, which extends the AppCompatTextView. That's working great with the autosize features. So if you need custom fonts with autosize, you can try it out. (similar to what's found here, but extending the AppCompatTextView instead of just normal TextViewDisorient
@JustinLiu Thanks! By the way, be careful of subclassing textview for custom font, caching of typeface need to be done or it will eat up a lot of system resources (createTypeFace is a heavy method and it will be called everytime when textview is created, if you don't cache it)Nonagon
@SiraLam Thanks for confirming that the new autosizing TextView features do not work with custom typefaces. I've been facing an issue getting the feature to work with a custom typeface and this is the only other resource I've found to confirm the issue.Cp
@SiraLam not sure if you'd still like to investigate this. However, from my experience, I haven't been able to autosizing to work with EditText. I have my EditText bounded in both width and height. I've tried to define autosizing both in XML and programatically and it doesn't appear to work.Oppress
D
11

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."

I also created a tutorial about Autosizing TextView here.

Dialysis answered 5/9, 2017 at 9:34 Comment(0)
W
10

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"/>
Watereddown answered 9/8, 2017 at 14:42 Comment(3)
Actually, you shouldn't have to use AppCompatTextView in your XML - the support library takes care of turning all your views in the AppCompat versions when inflating layouts.Plectrum
@Plectrum this is only true when targeting api 26 and higher however. there are "unexpected namespace prefix" warnings when using TextViewDarn
@Darn Refer to this method for avoiding the namespace warnings.Critical
P
6

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.

Pastille answered 15/2, 2018 at 0:24 Comment(1)
Thanks man for pointing that out. Made the same mistake and it drove me crazy !Manassas
Z
6

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.

Zacatecas answered 12/8, 2019 at 10:40 Comment(0)
F
6

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
Fca answered 25/9, 2020 at 8:19 Comment(0)
J
4

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

Japheth answered 6/7, 2017 at 14:44 Comment(0)
M
3

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
enter image description here

Micropaleontology answered 8/9, 2017 at 9:49 Comment(6)
Do you mean this feature works on older Android version?Inandin
@Inandin yes. it will work well on older version if you are using support library > 26Micropaleontology
For some reason I can't get it to work on Android 7.1.1, API 25 device, though it works just fine on an API 26 device. The text view works but it's not doing the autosizing magic unless running on Android 8.Inandin
This is wrong. Do not use wrap_content with AutoSize. The official documentation clearly states that: Note: 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.Odyssey
@MartinMarconcini we already have android:maxLines="1", so I think setting layout_height="wrap_content" is correct and it working wellMicropaleontology
@PhanVanLinh I’ve had the same scenario fail me in a nestedscrollview + RecyclerView scenario. Maybe it works for some cases, just be careful with it. In fact the documentation talks about “unexpected results”, so it’s not very clear about it. Mine was certainly unexpected.Odyssey
P
3

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"
Pleiades answered 15/4, 2021 at 10:20 Comment(0)
G
1

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

    }
Gath answered 27/9, 2017 at 6:3 Comment(0)
B
1

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" />
Broken answered 28/6, 2019 at 9:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.