Reputation: 153
I am trying to understand DisplayMetrics.DENSITY_DEVICE_STABLE
property on Android.
From Android 10 Compatibility Definition (and Android 7 Compatibility Definition):
[C-0-1] By default, device implementations MUST report only one of the Android framework densities that are listed on DisplayMetrics through the DENSITY_DEVICE_STABLE API and this value MUST NOT change at any time; however, the device MAY report a different arbitrary density according to the display configuration changes made by the user (for example, display size) set after initial boot.
Link: https://source.android.com/compatibility/android-cdd#7_1_1_3_screen_density
From my research, I found that on most devices, DisplayMetrics.DENSITY_DEVICE_STABLE
is equal to the resources.displayMetrics.densityDpi
when the screen zoom/display size is set to normal.
However on Samsung S10+ running Android 10 the DisplayMetrics.DENSITY_DEVICE_STABLE
is 420, while the resources.displayMetrics.densityDpi
is 640 with screen zoom set to normal.
I also, tested that on Samsung S8 running Android 8 and found that DisplayMetrics.DENSITY_DEVICE_STABLE
is equal to the resources.displayMetrics.densityDpi
with screen zoom set to default (it is 480).
So my question is: Should DisplayMetrics.DENSITY_DEVICE_STABLE
be the same as resources.displayMetrics.densityDpi
at default screen zoom.
Thank you
Upvotes: 3
Views: 1750
Reputation: 61
I had the same problem like you. It seems like you have to handle density changes for your activity yourself. When you add the following line to your manifest, then the app ui is resized according to the density and doesn't stay the same. So the values for DisplayMetrics.DENSITY_DEVICE_STABLE
and resources.displayMetrics.densityDpi
are both correct, but not visible in the UI, since the system handles scaling automatically.
Just add
android:configChanges="density|fontScale"
and change the resolution. Now you should be able to see that the UI is scaled "correctly".
Upvotes: 1
Reputation: 153
The DisplayMetrics.DENSITY_DEVICE_STABLE
on Samsung S10+ devices corresponds to the resources.displayMetrics.densityDpi
when the device is in FHD+ resolution and min display zoom.
It appears that when the device's resolution is changed from the default it impacts the resources.displayMetrics.densityDpi
and the DisplayMetrics.DENSITY_DEVICE_STABLE
property can no longer be safely compared to resources.displayMetrics.densityDpi
.
Upvotes: 1