Android Screen Adaptation (Modifying System Font Size, Display Size, Resolution Related Adaptation)

Android development will encounter a variety of models, so it is necessary to adapt to a variety of models. Common adaptions are not described in detail in this category. You can refer to the series of articles here. Android Universal Screen Adaptation The explanation is very detailed. Here's how to adapt wisely when changing the font size, display size and default resolution of the system.

First, modify the font size and display size of the system:

Settings - > Display - > Font and Display Size:

You will see the following two items:

1. Font size:

In our actual development, it is customary to use font unit (sp) as the default size unit. When SP is used as the font unit, the specific font size varies with the font size of the system. So SP can be used as the font size unit when the font you want to layout changes with the font size set by the system.

When the font unit you have used is sp and you don't want to change with the font size of the system settings, you can use the following method in Activity:

    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1) {
            //Non-default values

    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//Non-default values
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//set default
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        return res;


If you do not want the font size of the page layout to change with the font size of the system settings, you can directly use dp as the font size unit. At this time, the font size of the system is changed, and the font in the layout will not change. [In fact, dp is used as the font unit in many developing font sizes, because it can effectively avoid the UI layout disorder caused by changing font sizes].


2. Display size:

The display size is 7.0 or above, which is supported by the system (as shown above). (Of course, some mobile phone business systems automatically block this function, so you can't find it in the settings. Generally, Huawei mobile phones have this function.)

Of course, many people have to ask, when I change the display size, what is the specific change. In fact, the final change is DPI (dots per inch is the number of pixels per inch), and a very important formula between dp and px, dp = DPI / 160) * px, is calculated from dpi. So when the display size is increased, the DPI will also increase, so the number of pixels per dp will increase eventually. When the resolution of the system remains unchanged, the corresponding dp value will occupy a larger UI space (width/height), so the corresponding UI display will become larger.

If you want to modify the system's display size without changing the original UI layout size, you can use the following ways:

package com.hongri.layout.util;

import java.lang.reflect.Method;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.Log;
import android.view.Display;

 * Created by zhongyao on 2019-09-03.
public class DisplayUtil {
    private static final String TAG = DisplayUtil.class.getSimpleName();

     * The original UI style remains unchanged when the system sets "display size":
     * 1,When you adjust the display size of the mobile phone system, the corresponding dpi will become larger [dp = dpi / 160 * px]. At this time, the DP will become larger, so the corresponding UI layout will become larger.
     * 2,When the "resolution" of the mobile phone system is adjusted, the corresponding dpi decreases (e.g. from 480 - > 320). If we use technology to keep the dpi unchanged at this time, the same dp will occupy more px, so the UI layout will become larger.
     * @param context
    public static void setDefaultDisplay(Context context) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            Configuration origConfig = context.getResources().getConfiguration();
            //Get the default densityDpi when the mobile phone is out of the factory [Note 1]
            origConfig.densityDpi = getDefaultDisplayDensity();

            Log.d(TAG, "densityDpi: " + origConfig.densityDpi);
            context.getResources().updateConfiguration(origConfig, context.getResources().getDisplayMetrics());

    public static int getDefaultDisplayDensity() {
        try {
            Class clazz = Class.forName("android.view.WindowManagerGlobal");
            Method method = clazz.getMethod("getWindowManagerService");
            Object iwm = method.invoke(clazz);
            Method getInitialDisplayDensity = iwm.getClass().getMethod("getInitialDisplayDensity", int.class);
            Object densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY);
            return (int)densityDpi;
        } catch (Exception e) {
            return -1;

Then call it before Activity sets the layout:

You will find that the default UI display size is maintained even when the "display size" is adjusted. However, the use of this approach will lead to the following problems:

1. This approach will change the overall dpi (not just the Activity). So other pages will also be affected.

2. On the other hand, when modifying the resolution of the mobile phone system [some mobile phones support modifying the resolution of the mobile phone], such as reducing the resolution. So [Note 1] you still use the default resolution dpi, so according to the formula: DP = DPI / 160) * px, because DPI is still relatively large, the number of pixels per DP will now increase. Therefore, the current 1dp contains more pixels than the 1dp with smaller resolution. Therefore, the UI layout will be correspondingly larger, or even larger, which may lead to layout disorder or incomplete display.

In summary, it is not recommended to use technical means to control the display size of the system. [It has been proved that most of the APP s on the market do not control the display size, nor do they conform to user habits].


2. Modify the resolution:

Settings - > Display - > Screen Resolution:

In general, when the resolution is modified, there is no need for adaptation, because after the resolution is modified, the corresponding layout size will not change. In particular, as mentioned in the second part above, if you use technical means to control the display size of the system, there will be problems when the resolution is changed. The reason is that [for example, if you increase the display size and decrease the resolution], then the screen is of small resolution, but you have to use the default original (large resolution) dpi, so the layout will become larger.


Layout adaptation is a discipline. There will also be new changes in different systems and vendors. Need constant attention to learning in order to use freely!!!





Keywords: Android Mobile Java

Added by congos on Tue, 10 Sep 2019 09:23:38 +0300