0

Let's say we have a single-line text view with a fixed width. We want to display a string in it, but we don't know how long the string is yet.

We want the string to shrink as necessary in order to fit inside the text view without truncating.

In IOS, this accomplished with this:

BEFORE:

enter image description here

enter image description here

enter image description here

AFTER:

enter image description here

enter image description here

What is the Android equivalent?

I have consulted this and tried to test it out in code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tvc);
        tv.setText("aa aaa a aaaaaaa aaaadfgd gsfg fsdg dfs gdfs gds fg dsg dsg sdfg dsf g zzzzzzzzzz");

        TextViewCompat.setAutoSizeTextTypeWithDefaults(tv, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);


    }

but was dismayed to find that it is not shrinking the font automatically to fit width, but rather expanding the font to fit height... with the string being truncated even more.

Please note: this question is different than (Auto Scale TextView Text to Fit within Bounds) because it doesn't have the same requirements: I am talking strictly about a single-line textview with a width restriction... and none of the answers in that question are satisfactory.

Nerdy Bunz
  • 3,581
  • 2
  • 28
  • 60

2 Answers2

1

I suggest AutofitTextView. In Android, autofit TextView is not native supported so you have to customize.

Liar
  • 1,064
  • 1
  • 8
  • 19
0

either you use AutofitTextView or you can use the normal TextView with adding ellipsize property:

<TextView ...
    android:lines="1"
    android:ellipsize="end"
    />

This property will add three dots if the string length is more than the textview width.

Khalid Taha
  • 2,727
  • 2
  • 24
  • 40