Display size changed from system settings effects my layout Oreo

Viewed 1399

I want my layout to be of the same size and shape throughout and previously adjusting the font scale was working fine. Now android seems to have come up with the option of changing display size which is disorienting my whole layout.

I have tried making a values-large, layout-large and tried setting normal sizes to the layout but to no positive result.

I also found this code in some thread to help me with my problem:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Log.d("TAG", "adjustDisplayScale: " + configuration.densityDpi);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        if (configuration.densityDpi >= 485) //for 6 inch device OR for 538 ppi
            configuration.densityDpi = 500; //decrease "display size" by ~30
        else if (configuration.densityDpi >= 300) //for 5.5 inch device OR for 432 ppi
            configuration.densityDpi = 400; //decrease "display size" by ~30
        else if (configuration.densityDpi >= 100) //for 4 inch device OR for 233 ppi
            configuration.densityDpi = 200; //decrease "display size" by ~30
        Log.d("TAG", "metrics density: " + metrics.density + "," + metrics.scaledDensity);
        metrics.scaledDensity = configuration.densityDpi * metrics.density;
        Log.d("TAG", "scaledDensity: " + metrics.scaledDensity);
        this.getResources().updateConfiguration(configuration, metrics);
    }

This also doesn't seem to do any good to my layout. It still becomes huge and disoriented. Can anybody tell me what I can do to maintain the same size throughout without the system settings affecting my layouts and design?

2 Answers

use constraint-layout, there is no need of create large and xlarge layout if you use constraint-layout. And most important thing is use sp and dp.

ex. <dimen name="dm_40sp">40sp</dimen>

Sp automatically adjust size according to screen resolution.

constraint-layout is more easier and more effective then relative layout. Try this, if any quires ask me.

This won't be the answer you're looking for but still I'd like to share my opinion about it.

First of all, these settings are there for a reason. It's the accessibility. People with disabilities or difficulties uses that settings to adjust the UI according to their needs. So you're not supposed to find a way to overcome these settings at all.

Additinaly, if you haven't, you should read Support different screen sizes. It'll give you perspective about which layout is picked up by the system and when.

But if you don't care at all, then for the font sizes if you use dp instead of sp they will not be affected by the system setting. For the zooming level there is no recipe. You need to deceive the system like in your example code and attach that updated configuration on your app level and activity level. IMHO you shouldn't try to do anything at all, it is the will of the user to increase font size and zoom level, just leave them alone :)

After all, if your UI is ruined by these settings it means your UI is not adaptive or responsive, and you should update your UI and not try to disable these settings.

Related