Table of contents |
|
Autosizing TextView in Android |
|
Step by Step Implementation |
|
AutoSizing TextView in Android |
|
Android Development Notes |
|
Content Summary |
|
If a user inputs text that exceeds the screen size in an Android app, the TextView should gradually decrease in font size to accommodate the text. This article explores how developers can implement this feature to dynamically adjust the TextView size based on user input. Refer to the GIF below for a visual representation of the end result. Sample code is provided in both Java and Kotlin for Android development.
This section covers the implementation of autosizing TextView in Android using a dial pad as an example. The technique showcased can be applied in various scenarios such as building calculators where dynamic TextView resizing is necessary.
<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" > |
<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" /> |
Within the aforementioned code, one can observe attributes of the TextView such as 'app:autoSizeMaxTextSize="80sp"', representing the initial text sizes of the TextView.
AutoSizing TextView in Android allows dynamic text resizing for better readability and user experience.
It's important not to set the height and width of the TextView as wrap_content to ensure proper auto-resizing. To disable auto-resizing, set the attribute to none.
In the MainActivity File, onClick functions for each Button are handled individually to set the TextView accordingly.
android.annotation.SuppressLint | android.os.Bundle | android.view.View | android.widget.TextView |
androidx.appcompat.app.AppCompatActivity |
MainActivity : AppCompatActivity() { private lateinit var primaryTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) primaryTextView = findViewById(R.id.primaryTextView) } @SuppressLint("SetTextI18n") fun clickedOne(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "1" } @SuppressLint("SetTextI18n") fun clickedTwo(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "2" } @SuppressLint("SetTextI18n") fun clickedThree(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "3" } @SuppressLint("SetTextI18n") fun clickedFour(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "4" } @SuppressLint("SetTextI18n") fun clickedFive(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "5" } @SuppressLint("SetTextI18n") fun clickedSix(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "6" } @SuppressLint("SetTextI18n") fun clickedSeven(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "7" } @SuppressLint("SetTextI18n") fun clickedEight(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "8" } @SuppressLint("SetTextI18n") fun clickedNine(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "9" } @SuppressLint("SetTextI18n") fun clickedZero(view: View?) { primaryTextView.text = primaryTextView.text.toString() + "0" } }
Output: Run on Emulator