Android Autosizing TextViews Preset Sizes

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

In Java:

Call the setAutoSizeTextTypeUniformWithPresetSizes() method:

setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)

In XML:

Use the autoSizePresetSizes attribute in the layout XML file:

<TextView android:id=”@+id/autosizing_textview_presetsize” 
        android:layout_width=”wrap_content” 
        android:layout_height=”250dp” 
        android:layout_marginLeft=”0dp” 
        android:layout_marginTop=”0dp” 
        android:autoSizeText=”uniform” 
        android:autoSizePresetSizes=”@array/autosize_text_sizes” 
        android:text=”Hello World!” 
        android:textSize=”100sp” 
        app:layout_constraintLeft_toLeftOf=”parent” 
        app:layout_constraintTop_toTopOf=”parent” />

To access the array as a resource, define the array in the res/values/arrays.xml file:

<array name=”autosize_text_sizes”>
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
</array>

Check out the AutosizingTextViews-Demo at GitHub for more details.



Got any Android Question?