'How to resolve hint character wrapping on autosizing TextView with empty text on android?

I want to use autosizing TextView for dynamic text content.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:autoSizeTextType="uniform"
    android:hint="hint"
    android:text="some dynamic text" />
</LinearLayout>

This works so far:

text is not empty

But there is an issue if the dynamic text content is empty. The TextView wraps each character of the hint content to a new line:

text is empty

I expect hint to behave like text. How to resolve this without hacks like setting text content to hint content if the former is empty.



Solution 1:[1]

The same event happened to me.

I defined this and it fixed it.

app:autoSizeStepGranularity="1dp"

I'd definitely set the minimum and maximum sizes together.

android:hint="no message"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="1sp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"

Solution 2:[2]

layout.xml

   <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_height="match_parent"
     android:layout_width="match_parent"
     tools:context=".MainActivity">

   <TextView
      android:id="@+id/show"
      android:layout_width="match_parent"
      android:layout_height="350dp"
      android:hint="no message"
      />
  </RelativeLayout>

MainActivity.java

        TextView  shows =  findViewById(R.id.show);

        TextViewCompat.setAutoSizeTextTypeWithDefaults(shows,
                TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

        if(message.isEmpty()){

        shows.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

        }else{

        shows.setText(message);
        shows.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

         }

if you add this code please run the code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sasa-nori
Solution 2 Nagendran P