Autosizing TextViews for labels
ElyDantas opened this issue · 0 comments
Autosizing TextViews
With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content.
The Support Library 26.0 provides full support to the autosizing TextView feature on devices running Android versions prior to Android 8.0 (API level 26). The library provides support to Android 4.0 (API level 14) and higher. The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion.
To define the default setting in XML through the support library, use the app namespace and set the autoSizeTextType attribute to none or uniform.
Making every TextView
label align to it's rating bar's top and bottom and using AutoSizeText
attributes we can make it not only backwards compatible but auto resizable, without the need to set some static value for bar:text_size
:
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@+id/bar"
android:layout_alignBottom="@+id/bar"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="1sp"
app:autoSizeMaxTextSize="112sp"
app:autoSizeStepGranularity="1sp" />
Preventing a behaviour like this: