Disabling an app or activity zoom if Setting -> Display -> Display size changed to Large or small
Asked Answered
C

3

8

In my app, I don't want to allow it to resize as its creating design issues.

I have tried with android:resizeableActivity="false" in the application tag and the launcher activity tag but it didn't help.

Classmate answered 9/5, 2018 at 11:35 Comment(0)
C
25

I have found a solution for it.

If system text size changed or Display size set to Large (Above Android Oreo), Your app will behave as normal (No big text and and zoomed views) with below code:

        Configuration configuration = getResources().getConfiguration();
        configuration.fontScale = (float) 1; //0.85 small size, 1 normal size, 1,15 big etc
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        configuration.densityDpi = (int) getResources().getDisplayMetrics().xdpi;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
Classmate answered 12/5, 2018 at 12:4 Comment(1)
Quite a hacky solution, but it does work for me. Make sure you put this in onCreate before setContentView or it won't work after a screen rotationVilayet
M
1

I will assume you're trying to get display which doesn't change if user changes Accessibility Options on it's phone regarding UI size.

You can get this working by using DisplayMetrics.

DisplayMetrics metrics = getResources().getDisplayMetrics();

With "normal" scale factor values metrics.xdpi and metrics.densityDpi have the same value. If this is not the case, you can get "real" scale factor, that is one used when user uses normal scaling using following.

if(metrics.xdpi != metrics.densityDpi){
        Log.d(TAG,"Real scale " +  (metrics.xdpi / metrics.densityDpi)*metrics.density);
}

Than you can use said value to multiply your fixed values from layout so that it can have precise density on different screens. This would also mean that every value in layout must be using px instead of dp or sp.


While this approach would work, I would strongly recommend not to use it.

Firstly, because it is a lot of work, and it will be hard to maintain that code with future updates.

Secondly, this feature of Android is very convenient, and if your app doesn't compile with it it probably points out that you should construct your layout better.

Moua answered 9/5, 2018 at 12:53 Comment(1)
@Classmate was this answer helpful to you ?Moua
K
1

the answer here is only partly true, since xdpi and densityDPI coincide only in emulators, xdpi is different for all phones, and densitydpi is the same, so it would be wiser to do this:

val configuration = getResources().getConfiguration()
    val metrics = getResources().getDisplayMetrics()
    Log.d("TAG", metrics.toString() + " " + getResources().getConfiguration())
    when (resources.displayMetrics.xdpi) { //change size to ignore display size
        in 1..120 -> {
            configuration.densityDpi = 120
        }
        in 121..160 -> {
            configuration.densityDpi = 160
        }
        in 161..240 -> {
            configuration.densityDpi = 240
        }
        in 241..320 -> {
            configuration.densityDpi = 320
        }
        in 321..480 -> {
            configuration.densityDpi = 480
        }
        in 481..640 -> {
            configuration.densityDpi = 640
        }
    }
    getBaseContext().getResources().updateConfiguration(configuration, metrics)
Kletter answered 22/4, 2020 at 17:32 Comment(2)
This code does not compiles! Could you please share this same concept that compiles? Thank you.Elnora
Never mind it works just needed a null !! check in the end of resources.displayMetrics.xdpi.Elnora

© 2022 - 2025 — McMap. All rights reserved.