Disabling an app or activity zoom if Setting -> Display -> Display size changed to Large or small
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);
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.