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!
How to auto size textview dynamically according to the length of the text in android?
There's now an official solution to this problem. Autosizing TextViews introduced with Android O are available in the Support Library 26 and is backwards compatible all the way down to Android 4.0.
https://developer.android.com/preview/features/autosizing-textview.html
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;
}
Auto Scale TextView Text to Fit within Bounds - How to call?
Well if you have a class that describes how to make and modify an object, all you need to do is make it and modify it with its constructor and methods. Below is the one from your link.
public FontFitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
float size = this.getTextSize();
if (size > MAX_TEXT_SIZE)
setTextSize(MAX_TEXT_SIZE);
}
You call in your activity FontFitTextView textViewName = new FontFitTextView(context, attrs);
and then add it to a view by layoutName.addView(textViewName);
This means you need to find the view you are using for a layout by its android:id as I am assuming you dont want to build the whole layout programatically. You can also call the constructor in xml by <packagename.FontFitTextView />
tl;dr - Use the constructor
Related Topics
How to Create an Object of an Activity in Other Class
How to Dynamically Add Elements to a Listview on Android
Playing Html5 Video on Fullscreen in Android Webview
Install/Uninstall Apks Programmatically (Packagemanager VS Intents)
What's the Difference Between Commit() and Apply() in Sharedpreferences
"Protected Apps" Setting on Huawei Phones, and How to Handle It
How to Check the Current Status of the Gps Receiver
Detect If Android Device Has Internet Connection
How to Change Current Theme At Runtime in Android
Android Gradle Plugin 0.7.0: "Duplicate Files During Packaging of Apk"
Scheduling Recurring Task in Android
Set Style For Textview Programmatically
Determining the Size of an Android View At Runtime
Adb Server Version Doesn't Match This Client
How to Detect Orientation Change in Layout in Android
What Is the Android Uithread (Ui Thread)
How to Add a Footer in Listview
How to Pass Images (Bitmaps) Between Android Activities Using Bundles