I'm having a hard time figuring out the problem with autoSizeText used in TextView.
Info: I have an activity that will show app tips to the user. Tips will automatically change every n seconds. The problem I have is with TextView (android:id="@+id/tipDescription") that has autoSizeText.
The default textSize is 30sp, maxTextSize is also 30sp, and minTextSize is 10sp.
When the TextView with autoSizeText sets text with a lot of characters, the textSize will reduce which is great. The problem occurs when the textSize stays the same (small) even when the text in TextView is back to normal lengths that fit the View.
Example:
Text 1: "Start text" - textSize is 30sp
Text 2: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s." - textSize is reduced accordingly.
Text 3: "End text" - textSize is now the same as with Text 2, which is too small.
I am looking for a solution that will reset the textSize (back to 30sp) when the text is back to normal length.
XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="40dp"
android:background="#444"
android:padding="12dp"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/tipTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TITLE"
android:textColor="#FF00FF"
android:textSize="26sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tipSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="SUBTITLE"
android:textColor="#80FFFFFF"
android:textSize="14sp" />
<TextView
android:id="@+id/tipDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="Start text"
android:textColor="#FFF"
android:textSize="30sp"
app:autoSizeMaxTextSize="30sp"
app:autoSizeMinTextSize="10sp"
app:autoSizeTextType="uniform" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java (MainActivity.java)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tipDescription = findViewById(R.id.tipDescription);
new Handler().postDelayed(() -> tipDescription.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."), 2000);
new Handler().postDelayed(() -> tipDescription.setText("End text"), 4000);
}
}
I have already tried tipDescription.setTextSize but it's not working with autoSizeText enabled.
I'm using android:layout_height="wrap_content" to keep everything vertically centered in parent LinearLayout (tipSubtitle below tipTitle and tipDescription below tipSubtitle).