Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active November 3, 2019 01:05
Show Gist options
  • Save juliuscanute/b08ab7bbc356114ad00d1cd36524305f to your computer and use it in GitHub Desktop.
Save juliuscanute/b08ab7bbc356114ad00d1cd36524305f to your computer and use it in GitHub Desktop.
[Autosizing TextView] #android #autosize #textview #runtime #uniformsize #granualaritysize #presetsize
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="autosize_text_sizes">
<item>12sp</item>
<item>16sp</item>
<item>20sp</item>
<item>24sp</item>
<item>28sp</item>
</array>
</resources>
<!--Uniform Scaling-->
...
<TextView
...
app:autoSizeTextType="uniform"
...
/>
...
<!--Granualarity-->
...
<TextView
...
app:autoSizeMaxTextSize="28sp"
app:autoSizeMinTextSize="12sp"
app:autoSizeStepGranularity="4sp"
...
/>
...
<!--Preset Size-->
...
<TextView
...
app:autoSizePresetSizes="@array/autosize_text_sizes"
...
/>
...
Starting with Android 8.0 (API level 26), you can set these new autosizing properties with the TextView class. Thanks to the Support Library 26.0, you can also use these on devices running Android 4.0 (API level 14) or later by using the TextViewCompat class.
//Uniform Scaling
...
TextViewCompat.setAutoSizeTextTypeWithDefaults(this,
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
...
//Granualarity
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this,
12, 28, 4,
TypedValue.COMPLEX_UNIT_SP)
...
//Preset Size
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(this,
context.resources.getIntArray(R.array.autosize_text_sizes),
TypedValue.COMPLEX_UNIT_SP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment