How to Scale/Resize Text to Fit a Textview

Auto Scale TextView Text to Fit within Bounds

From June 2018 Android officially started supporting this feature for Android 4.0 (API level 14) and higher.
Check it out at: Autosizing TextViews

With Android 8.0 (API level 26) and higher:

<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp" />

Programmatically:

setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, 
int autoSizeStepGranularity, int unit)

textView.setAutoSizeTextTypeUniformWithConfiguration(
1, 17, 1, TypedValue.COMPLEX_UNIT_DIP);


Android versions prior to Android 8.0 (API level 26):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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">

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />

</LinearLayout>

Programmatically:

TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
TextView textView, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)

TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView, 1, 17, 1,
TypedValue.COMPLEX_UNIT_DIP);

Attention: TextView must have layout_width="match_parent" or absolute size!

Auto Scale Text Size

You don't need to call AutoResizeTextView test, you can say TextView since the class extends TextView. I don't see why you'd need to call resizeText() either.

Either way, here's a custom class I like to use to auto re-size text.

public class AutoFitTextView extends TextView {

public AutoFitTextView(Context context) {
super(context);
init();
}

public AutoFitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {

maxTextSize = this.getTextSize();
if (maxTextSize < 35) {
maxTextSize = 30;
}
minTextSize = 20;
}

private void refitText(String text, int textWidth) {
if (textWidth > 0) {
int availableWidth = textWidth - this.getPaddingLeft()
- this.getPaddingRight();
float trySize = maxTextSize;

this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
while ((trySize > minTextSize)
&& (this.getPaint().measureText(text) > availableWidth)) {
trySize -= 1;
if (trySize <= minTextSize) {
trySize = minTextSize;
break;
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
}
}

@Override
protected void onTextChanged(final CharSequence text, final int start,
final int before, final int after) {
refitText(text.toString(), this.getWidth());
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw) {
refitText(this.getText().toString(), w);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
refitText(this.getText().toString(), parentWidth);
}

public float getMinTextSize() {
return minTextSize;
}

public void setMinTextSize(int minTextSize) {
this.minTextSize = minTextSize;
}

public float getMaxTextSize() {
return maxTextSize;
}

public void setMaxTextSize(int minTextSize) {
this.maxTextSize = minTextSize;
}

private float minTextSize;
private float maxTextSize;

}

Scale text in a view to fit?

You can use the TextUtils.EllipsizeCallback. When the text gets ellipsized this callback is done by the textview. Here you can set text size smaller than the current.

EDIT : Otherwise you can use TextUtils.ellipsize this way

while (mText != TextUtils.ellipsize(mText, textPaint, other params)) { 
textpaint.setTextSize(textpaint.getTextSize() - 1);
}

Text scale up to fit a TextView and an EditText

You are already using it correctly. By defining it in your XML, you tell the inflater to use that class.

If you want to access it in Java, treat it like any other view:

    AutoResizeTextView textView = (AutoResizeTextView) findViewById(R.id.textView1);
textView.setText("Hello, user!");

It will behave just like any other text view, except it is supposed to automatically resize the text.

How to resize text size depending on widget size in a textview in an appwidget?

It's not possible to automatically scale the text of a TextView based on parent size. A workaround would be to override the onDraw method of the TextView and implement the scaling manually.

As for the appwidgets: You can't retrieve a resizable appwidget's current size because they are intended for use with ListView and GridView containers. These containers will manage the scaling and spacing of content automatically and will make themselves scrollable if the content can't fit in the appwidget's current size.

If you're not using a ListView or a GridView it seems to me the best way to "scale" the components of a appwidget is the method you already mentioned: Use different non-resizeable appwidgets.



Related Topics



Leave a reply



Submit