How to automatically adjust text view dynamically according to text length in android?

I take variable-sized data from the server and set it to textView, and I wanted the text image to change according to the length of the typing.

It is provided in the material development guides. How to do it?

Material Design Guidelines

Guide link - https://material.google.com/style/typography.html#typography-other-typographic-guidelines

+11
android android-layout
source share
6 answers

There is now an official solution to this problem. AutoSize TextViews introduced in Android O is available in support library 26 and is backward compatible up to Android 4.0.

https://developer.android.com/preview/features/autosizing-textview.html

+10
source share

addTextChangedListener is a listener for Edit Tex. this listener observes editText changes and has three different states.

EditText edt = someEditText; edt.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } /*watch the editText on every input and changes, then mange the size by if statements and editText length*/ @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (edt.getText().toString().length() > 10){ edt.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall); } else if (edt.getText().toString().length() > 5){ edt.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium); } } @Override public void afterTextChanged(Editable s) { } }); 

Updated: According to the question, you can create a component (custom view) and extend it from the name AppCompatTextView as you wish; in its initialization you can add the code below:

 public class CustomTextView extends AppCompatTextView { Context ctx; public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); ctx = context; init(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); ctx = context; init(); } public CustomTextView(Context context) { super(context); ctx = context; init(); } public void init() { setOnTouchListener(null); addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (getText().toString().length() > 10){ setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall); } else if (getText().toString().length() > 5){ setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium); } } @Override public void afterTextChanged(Editable s) { } }); } 

}

you should use it in xml instead of regular textView

+4
source share

Perhaps you can try something like this:

 EditText et = someEditText; String text = et.getText().toString(); int textLength = text.length(); if (textLength > 25) { et.setTextSize(toSomeNumber as the size of the text); } else if (textLength > 15) { et.setTextSize(toSomeNumber bigger than the previous); } else { et.setTextSize(toSomeNumber this is the biggest); } 

UPDATE: You can refer to this question if you want to solve your problem differently. How to set text size dynamically for different screens

This is what I use for my own codes. Any suggestions are accepted to improve my code. Hope this helps.

+1
source share

Here is one way to do this using the support library:

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:breakStrategy="balanced" android:gravity="center_horizontal" android:maxLines="1" android:text="Hello world" android:textSize="300sp" app:autoSizeTextType="uniform" tools:targetApi="o"/> 

android:breakStrategy allows android:breakStrategy to make text go well to the next line instead of the default behavior that can break words.

In Gradle use this:

 implementation 'androidx.appcompat:appcompat:1.0.0-beta01' implementation 'com.google.android.material:material:1.0.0-beta01' 

Or that:

 implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' 

Please note that it is recommended that you set layout constraints for the TextView (width and / or height) to make sure that you do everything correctly. It all depends on your use case.

+1
source share

Use the library below that is perfect for your needs.

According to the document: TextView, which automatically resizes the text to fit its borders.

Link: Android autofit textview

0
source share

In version 26.0.1, the Support Library adds support for autosave in AppCompatTextView .

Developers can now let their text size expand or shrink automatically depending on the size and characteristics of the TextView, making it easier to optimize text size on different screens or dynamic content.

Grain

In Java:

Call the setAutoSizeTextTypeUniformWithConfiguration() method:

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

In XML:

Use the autoSizeMinTextSize, autoSizeMaxTextSize, and autoSizeStepGranularity attributes to set automatic calibration parameters in the layout XML file:

 <android.support.v7.widget.AppCompatTextView android:id="@+id/autosizing_textview_presetsize" android:layout_width="wrap_content" android:layout_height="250dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" app:autoSizeMaxTextSize="100sp" app:autoSizeMinTextSize="12sp" app:autoSizeStepGranularity="2sp" app:autoSizeText="uniform" android:text="Hello World!" android:textSize="100sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> 

Preset sizes

In Java:

Call the setAutoSizeTextTypeUniformWithPresetSizes() method:

 setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) 

In XML:

Use the autoSizePresetSizes attribute in the XML layout file:

 <android.support.v7.widget.AppCompatTextView android:id="@+id/autosizing_textview_presetsize" android:layout_width="wrap_content" android:layout_height="250dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" app:autoSizeText="uniform" app:autoSizePresetSizes="@array/autosize_text_sizes" android:text="Hello World!" android:textSize="100sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> 

To access the array as a resource, define the array in res / values ​​/arrays.xml:

 <array name="autosize_text_sizes"> <item>10sp</item> <item>12sp</item> <item>20sp</item> <item>40sp</item> <item>100sp</item> </array> 
0
source share

All Articles