Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Introduced in Android 8.0 (API level 26), autosizing TextView
allows developers to make text dynamically adjust its size based on the boundaries of its layout. Instead of fixing the text size, you can specify how the text size should adjust to fit its dimensions.
XML Implementation:
You can specify the autosizing directly in the XML:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="12sp" android:autoSizeMaxTextSize="100sp" android:autoSizeStepGranularity="2sp" android:text="This is an autosizing TextView example." />
Key attributes:
autoSizeTextType
: Specifies the type of autosizing. uniform
means it will resize uniformly across multiple lines.autoSizeMinTextSize
: The minimum text size.autoSizeMaxTextSize
: The maximum text size.autoSizeStepGranularity
: The interval to jump between sizes during autosizing. For example, 2sp
means it will try 12sp
, 14sp
, 16sp
, and so on.Preset Sizes:
Instead of providing a range (min, max) and granularity, you can also specify specific text sizes that the TextView
should use:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:autoSizePresetSizes="@array/presetSizes" android:text="This is another autosizing TextView example." />
In res/values/arrays.xml
:
<resources> <array name="presetSizes"> <item>12sp</item> <item>20sp</item> <item>40sp</item> </array> </resources>
Programmatic Implementation:
You can also control autosizing programmatically:
val textView: TextView = findViewById(R.id.textView) textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)
For more control, such as when specifying a range:
textView.setAutoSizeTextTypeUniformWithConfiguration( 12, // Minimum size 100, // Maximum size 2, // Step granularity TypedValue.COMPLEX_UNIT_SP // Unit )
Or when using preset sizes:
val sizes = intArrayOf(12, 20, 40) textView.setAutoSizeTextTypeUniformWithPresetSizes(sizes, TypedValue.COMPLEX_UNIT_SP)
Autosizing TextView
is particularly useful when you're unsure of the amount of content that will be in a TextView
or when you're dealing with dynamic content sizes and want to ensure text remains legible and fits within its bounds.
Remember, while autosizing can be a powerful feature, you should always check and test the final appearance on various devices and screen sizes to ensure legibility and a consistent design.
How to use autosizing TextView in Android:
val textView = findViewById<TextView>(R.id.myTextView) TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM) TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration( textView, 8, 24, 2, TypedValue.COMPLEX_UNIT_SP )
Autosizing TextView with example in Kotlin:
val textView = findViewById<TextView>(R.id.myTextView) TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
Dynamic text size in Android TextView:
val textView = findViewById<TextView>(R.id.myTextView) textView.text = "Dynamic Text Example" textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)
Programmatically set autosizing TextView in Android:
val textView = findViewById<TextView>(R.id.myTextView) textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM) textView.setAutoSizeTextTypeUniformWithConfiguration(8, 24, 2, TypedValue.COMPLEX_UNIT_SP)
Using autosizeTextType attribute in TextView:
autosizeTextType
attribute in XML to define the autosizing behavior.<TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:autosizeTextType="uniform" app:autoSizeMinTextSize="8sp" app:autoSizeMaxTextSize="24sp" app:autoSizeStepGranularity="2sp"/>
Autosizing TextView with XML attributes in Android:
<TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:autosizeTextType="uniform" app:autoSizeMinTextSize="8sp" app:autoSizeMaxTextSize="24sp" app:autoSizeStepGranularity="2sp"/>
TextView auto-sizing with support library:
val textView = findViewById<TextView>(R.id.myTextView) TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
Customizing autosizing behavior in Android:
val textView = findViewById<TextView>(R.id.myTextView) textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM) textView.setAutoSizeTextTypeUniformWithConfiguration(8, 24, 2, TypedValue.COMPLEX_UNIT_SP)