Search code examples
androidandroid-studioautoresize

How can I make TextView auto-size in android?


I would like to implement the method that auto-resize the text for better compatibility.

I followed this article:

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

and I write this code:

<TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoSizeMaxTextSize="100sp"
        android:autoSizeMinTextSize="12sp"
        android:autoSizeStepGranularity="2sp"
        android:autoSizeTextType="none"
        android:fontFamily="@font/arimo"
        android:text="Ti piace Youtube?"
        android:textAlignment="viewStart"
        android:textAllCaps="false"
        android:textColor="#F8F3F3"
        android:textSize="20sp"
        android:textStyle="bold"
        app:autoSizePresetSizes="@array/autosize_text_sizes" />

What I'm wrong? the text doesn't auto-size and, for example, in Nexus S Emulator, I can't see the text full but it is cut.

thank Elvis


Solution

  • I use ssp and sdp. The ssp a scalable size unit for texts is android SDK that provides a new size unit - ssp (scalable sp). This size unit scales with the screen size based on the sp size unit (for texts). It can help Android developers with supporting multiple screens.and sdp This is the sibling of the sdp size unit that should be used for non text views. https://github.com/intuit/ssp and https://github.com/intuit/sdp use gradle and compile libraries like below

    implementation 'com.intuit.sdp:sdp-android:1.0.4'
        implementation 'com.intuit.ssp:ssp-android:1.0.4'
    

    in your layout activity xml for example for text you can provide like this

    android:textSize="@dimen/_16ssp" for layouts

    android:layout_height="@dimen/_16sdp" this will take care for any screens enjoy.

    for text only add implementation 'com.intuit.ssp:ssp-android:1.0.4' on gradle file then here is the example on your xml file in TextView implement it like below

    <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Intuit"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/_40ssp"/>