2
<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="start"
      android:lines="1"
      android:maxLines="1"
      app:autoSizeMinTextSize="20sp"
      app:autoSizeMaxTextSize="28sp"
      app:autoSizeTextType="uniform"
      app:layout_constrainedWidth="true" />

Let's say the text size of the TextView is set to minimum (20sp) after resizing and setting the text to for example XXXXXXXXXXXXXXXXXXXXXX.

Then, I would like to set a new text which is XXX where I expect that the TextView restores it's MaxTextSize. However, the behavior is not as expected and the text size stays the same (minimum (20sp)) since the TextView cannot determine its auto size width because of wrap_content.

Is it possible to invalidate or reset the TextView to it's initial state for it to determine what text size should be applied?

Note: It is required that the TextView should have wrap_content as its width and I am aware that the documentation states that wrap_content and auto-resizing sometimes results into an unexpected behavior.

2 Answers2

1

A good approach that came up with me was giving minHeight and minWidth to the TextView so it could calculate it's dimensions properly and as expected.

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:minHeight="46dp"
  android:minWidth="120dp"
  android:gravity="center_vertical"
  android:lines="1"
  android:maxLines="1"
  app:autoSizeMinTextSize="20sp"
  app:autoSizeMaxTextSize="28sp"
  app:autoSizeTextType="uniform"
  app:layout_constrainedWidth="true" />

Nevertheless, the best answer is found here. Auto Scale TextView Text to Fit within Bounds

1

For me, width set to wrap_content was the problem. I was using ConstraintLayout. Setting width to 0dp fixed it...

Peter
  • 340
  • 4
  • 13