androidandroid-layouthuawei-mobile-servicesscreen-density

Problem with app layout for phones with different screen densities


I have problem with layout of my app for below phones . in first one layout looking good but in the second phone the layout not exactly like first one . though i put the xml files in (Layout-w480dp) folder and i do not use constraint layout

huawei G8 : Photo Resolution : 1920 x 1080 px Screen Density : 403 ppi Screen Density (DPI) : 480 ppi (xxhdpi) (640dp) x (360dp)

huawei Y9 : Photo Resolution : 2160 x 1080 px Screen Density : 407 ppi Screen Density (DPI) : 480 ppi (xxhdpi) (720dp) x (360dp)

thanks


Solution

  • add this attribute to your textView in xml app:autoSizeTextType="uniform" this will resize the text size to fit the textview area

      <TextView
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:text="Hello World!"
        app:autoSizeTextType="uniform"    
        tools:ignore="MissingPrefix" />
    

    for further customizing you can set

        app:autoSizeMaxTextSize="100sp"
        app:autoSizeMinTextSize="20sp"
        app:autoSizePresetSizes="@array/autosize_text_sizes"
    

    where autosize_text_sizes is an array in arrays.xml values res file create it by right click on values -> new -> values resource file -> name it arrays.xml, which choose dynamically the text size based on text area availability

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
    <array name="autosize_text_sizes">
        <item>10sp</item>
        <item>50sp</item>
        <item>150sp</item>
    </array>