16

I am taking data of variable sizes from the server and setting to a textView and i wanted the textview to resize according to the length of the text set.

It is given in the material design guidelines. How to go about coding it ?

Material Design Guidelines

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

Jobin Lawrance
  • 538
  • 1
  • 5
  • 10

7 Answers7

18

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

Artem Russakovskii
  • 20,170
  • 17
  • 87
  • 114
9

Here's one way to do it, using the support library:

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

The android:breakStrategy allows making the text go to the next line nicely, instead of the default behavior which might break the words.

In gradle, use this:

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

Or this:

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

Note that it's recommended to set layout restrictions on the TextView (width and/or height), to make sure you will get it right. All depends on your use case.

android developer
  • 106,412
  • 122
  • 641
  • 1,128
  • 1
    autosize + wrap_content are discouraged and may produce unexpected results: https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html – Kikiwa Mar 06 '19 at 11:13
  • @Kikiwa Makes sense. Here it's just an example. You should always have the auto-resizing have rules of some kind, so that it will know how to auto-resize. Everything depends on the layout that this TextView exists in. – android developer Mar 06 '19 at 12:48
  • 1
    https://android.jlelse.eu/auto-sizing-textviews-in-android-d615ee2dc794 – Narendra Mar 26 '19 at 14:01
5

addTextChangedListener is a listener for Edit Tex. this listener watch the changes of editText and it 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 question you can create a component(custom view) and extend it from AppCompatTextView name as you want; in its initialization you can add below code:

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 must use it in xml instead of the usual textView

Payam Kokabi
  • 214
  • 1
  • 2
  • 13
4

Revision 26.0.1 of Support Library added support for autosizing in AppCompatTextView.

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

Granularity

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 the auto-sizing dimensions 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 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: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 the res/values/arrays.xml file:

<array name="autosize_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
</array>
sud007
  • 4,289
  • 2
  • 49
  • 56
Darish
  • 8,638
  • 2
  • 37
  • 57
  • +1 for setAutoSizeTextTypeUniformWithConfiguration, although the docs are sparse, the parameters for it listed were helpful. Unit where unit is one of the TypedValue constants i.e. .COMPLEX_UNIT_SP or others. – josh527 Jun 26 '20 at 17:37
1

Maybe 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 in more android way. How to set text size of textview dynamically for different screens

This is what I use for my own codes. Any suggestion is accepted to enhance my code. Hope it helps.

Community
  • 1
  • 1
Recomer
  • 160
  • 1
  • 12
0

Bear in mind that this does break accessibility. For example, if user decides to change font size through phone settings, because he simply cannot see very well, autosized text views with height set as DP won't be impacted.

In order to work around this, you should use android:layout_height with SP, so that it would also be scaled, when accessibility settings were changed.

-1

Use below library which is perfect for your needs.

According to doc: A TextView that automatically resizes text to fit perfectly within its bounds.

Link: Android autofit textview

Smit.Satodia
  • 587
  • 4
  • 12