![]() |
VOOZH | about |
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, it has 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 better, what we are going to build at the end of this article. The code has been given in both Java and Kotlin Programming Language for Android.
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.
To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. Rename the application name as Auto resize TextView, let the name of the layout be activity_main.xml, and select Java or Kotlin as Language.
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.
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:
Now in the MainActivity 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:
Output: Run on Emulator