Autosizing TextView in Android - GeeksforGeeks

18 Feb, 2021
If the user is giving the input and the input needs to be shown as TextView and if the user inputs the stuff which can go out of the screen, then in this case the font TextView should be decreased gradually. So, in this article its been discussed how the developer can reduce the size of TextView gradually as the user gives the data. One can have a look at the following GIF to understand it better, what we are going to build at the end of this article. Note that we are going to implement this project using the Java language.
In this article, the dial pad is taken as an example. This method can be used in building the calculator and can be used in many other scenarios where the TextView resize is required.
Step 1: Create an android studio project with an empty activity
You may refer to this Android | How to Create/Start a New Project in Android Studio on how to create an empty activity android studio project.
Rename the application name as Auto resize TextView, and let the name of the layout be activity_main.xml
And select Java as Language.
Step 2: Building an appropriate layout by working with the activity_main.xml file
As we have taken the dial pad as an example to demonstrate the auto-resizing of the TextView, so invoke the following code in the activity_main.xml file to make the UI like a dial pad.
XML
<?xml version="1.0" encoding="utf-8"?>  <!--the attributes further invoked for the      TextView are the part of Android Jetpac so we need to     include xmlns:app="http://schemas.android.com/apk/res-auto"      as you can see below--><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"                    xmlns:app="http://schemas.android.com/apk/res-auto"                    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity"    tools:ignore="HardcodedText">      <!--One needs to hardcode the height of the TextView-->    <!--so that it can decrease its height automatically within-->    <!--the given height and it doesn't disturbs the other widgets-->    <!--one can set the maxLines of the TextViews upto which-->    <!--the lines of the TextViews must be visible in this case its 2-->    <TextView        android:id="@+id/primaryTextView"        android:layout_width="match_parent"        android:layout_height="100dp"        android:hint="0"        android:maxLines="2"        app:autoSizeMaxTextSize="80sp"        app:autoSizeMinTextSize="10sp"        app:autoSizeStepGranularity="2sp"        app:autoSizeTextType="uniform" />      <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal"        android:weightSum="3">          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedOne"            android:text="1"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedTwo"            android:text="2"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedThree"            android:text="3"            android:textSize="24sp" />      </LinearLayout>      <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal"        android:weightSum="3">          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedFour"            android:text="4"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedFive"            android:text="5"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedSix"            android:text="6"            android:textSize="24sp" />      </LinearLayout>      <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal"        android:weightSum="3">          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedSeven"            android:text="7"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedEight"            android:text="8"            android:textSize="24sp" />          <Button            android:layout_width="68dp"            android:layout_height="68dp"            android:layout_margin="8dp"            android:onClick="clickedNine"            android:text="9"            android:textSize="24sp" />      </LinearLayout>      <Button        android:layout_width="68dp"        android:layout_height="68dp"        android:layout_gravity="center"        android:layout_marginTop="8dp"        android:onClick="clickedZero"        android:text="0"        android:textSize="24sp" />  </LinearLayout>
One can see attributes of TextView in above code app:autoSizeMaxTextSize=”80sp”, this is the initial sizes of TextView.
app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.
app:autoSizeTextType=”uniform” by using this attribute we are simply resizing the TextView uniformly.
One thing to remember is one shouldn’t set the height and width of the TextView as wrap_content.
To disable the resizing of the TextView, can set this attribute to none. As you can see below.
app:autoSizeTextType=”none”
The following Output UI is produced: 
Step 3: Now handling each Button separately, using the onClick XML attribute 
Now in the MainActivity.java file, we will handle all the onClick functions of each Button separately, and set the TextView to the appropriate number when pressed. So, now invoke the following code:
Java
import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;import android.os.Bundle;import android.view.View;import android.widget.TextView;  public class MainActivity extends AppCompatActivity {      TextView primaryTextView;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // register the button using appropriate ID of the TextView        primaryTextView = findViewById(R.id.primaryTextView);    }      // handle the button 1 and append the 1 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedOne(View view) {        primaryTextView.setText(primaryTextView.getText() + "1");    }      // handle the button 2 and append the 2 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedTwo(View view) {        primaryTextView.setText(primaryTextView.getText() + "2");    }      // handle the button 3 and append the 3 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedThree(View view) {        primaryTextView.setText(primaryTextView.getText() + "3");    }      // handle the button 4 and append the 4 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedFour(View view) {        primaryTextView.setText(primaryTextView.getText() + "4");    }      // handle the button 5 and append the 5 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedFive(View view) {        primaryTextView.setText(primaryTextView.getText() + "5");    }      // handle the button 6 and append the 6 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedSix(View view) {        primaryTextView.setText(primaryTextView.getText() + "6");    }      // handle the button 7 and append the 7 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedSeven(View view) {        primaryTextView.setText(primaryTextView.getText() + "7");    }      // handle the button 8 and append the 8 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedEight(View view) {        primaryTextView.setText(primaryTextView.getText() + "8");    }      // handle the button 9 and append the 9 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedNine(View view) {        primaryTextView.setText(primaryTextView.getText() + "9");    }      // handle the button 0 and append the 0 to the end of the TextView    @SuppressLint("SetTextI18n")    public void clickedZero(View view) {        primaryTextView.setText(primaryTextView.getText() + "0");    }}
adityamshidlyali
android
Android-View
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples