HughHughTeotl
HughHughTeotl

Reputation: 5789

Auto-sizing TextView does not size to fit updated text

I'm using an Android auto-sizing TextView via the appcompat library, as below:

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/info"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="8sp"
    app:autoSizeMaxTextSize="14sp"
    app:autoSizeStepGranularity="0.5sp"
    android:text="@string/something" />

This works nicely the first time the text is set - eg the text size shrinks if it needs to.

But the second time it's set, the text size doesn't grow again if the content is small enough to allow it. It remains at the smaller size appropriate for the previous content.

So, what is the proper procedure for calling setText on an auto-sizing TextView, so that the auto-sizing parameters are respected? Or is there a way to force the TextView to re-evaluate its content and resize the text?

I have tried setting the text size back to the default before calling setText but that seems to have no effect.

Upvotes: 3

Views: 617

Answers (1)

user8959091
user8959091

Reputation:

By reproducing your code I faced the same problem and the only solution that worked is giving a specific height to the TextView like: android:layout_height="30dp"
From: https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview#setting-textview-autosize

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.

Upvotes: 5

Related Questions