Last active
November 3, 2019 01:05
-
-
Save juliuscanute/b08ab7bbc356114ad00d1cd36524305f to your computer and use it in GitHub Desktop.
[Autosizing TextView] #android #autosize #textview #runtime #uniformsize #granualaritysize #presetsize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--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" | |
... | |
/> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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