3

On my last activity i get a textView and if is too big it just stays the same size and goes to the next line. Is there any way to have my textView shrink so it can fit on one line if it is too big ?

So instead of :

ABCDEFGHIJ (big font on two lines)
HIJK

I want:

abcdefghijk (smaller font on one line )

So it all fits from left to right on one line within the fone

XCarb
  • 735
  • 10
  • 31

5 Answers5

3

If you want to have autoresize on a TextView use this :

<TextView
        android:layout_width="60dp"
        android:layout_height="48dp" 
        android:lines="2"
        android:text="Hello world"
        android:autoSizeTextType="uniform"/>

What is very important is to not use android:singleLine="true" cos it's deprecated. Ans to have a fixed size so the autoresizing of the text will work correctly. You can also chose the number of line that you what.

Also note that with this the minimum size is 12sp, the maximum size is 112sp and the granularity is 1px. But if you want to custom those size, you can use :

android:autoSizeMinTextSize="8sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp"

That you can add to the above xml.

XCarb
  • 735
  • 10
  • 31
1

android:singleLine="true" this property in text view will help,it will not decrease the size of text but allow to show text in single line.

Anand Phadke
  • 531
  • 3
  • 28
1

This is a job for a custom View subclass. It’s very easy to create a View subclass, in this case you just need to override the onDraw method, since you don’t need to implement any special user interaction. Basically measure the width of the text at a reference point size, then choose a scaled point size based on that at which to draw the text.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
1

1) Put android:lines="1" in your textview
2) If you want 3dots at end of lines then put android:ellipsize="end" in your textview

Note:
Do not use android:singleLine="true" as it is deprecated now.

Jaymin Soni
  • 451
  • 3
  • 10
0

Declare android:autoSizeMinTextSize or app:android:autoSizeMinTextSize (in supoort library) with 1sp to reduce the minimum texsize. If the line is not too long (I assume is your case) you'll have a solution.

Sergio A
  • 71
  • 2
  • 3