I’m using the emulator in Android Studio to check my app on different device size categories - small, normal, large, xlarge. The size category in the emulator and that shown in code don’t seem to be consistent.
For example, going to the “Device Emulator”, “Create Device” and looking at the “Phone” list, the Pixel 5 is classified as:
Size: large, Ratio: long, Density 420dpi
I’m only interested in the Size as I’m using dimen.xml files for the categories “small”, “normal”, “large”, “xlarge”. So, I’m expecting this device to use the values for the “large” size. However, it seems to be detecting the size as “normal”.
I’ve then checked this by creating a blank app with this code that shows the device screen size as a Toast. I’ve taken this from How to determine device screen size category (small, normal, large, xlarge) using code? with the response from Thuy Trinh
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Toast.makeText(applicationContext, checkDeviceSize(), Toast.LENGTH_LONG).show()
}
private fun checkDeviceSize(): String {
val screenLayout = resources.configuration.screenLayout
return when {
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_XLARGE -> "Xlarge"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_UNDEFINED -> "Undefined"
else -> error("Unknown screenLayout: $screenLayout")
}
}
}
When I run it with the Pixel 5 which, as mentioned above, should be “large”, the Toast says it’s “normal”.
I’ve tried this with a few other devices, including ones I’ve created myself, and the same problem often, but not always, occurs.
I then created an emulator for a phone I have which is a Samsung Galaxy A22 5G. According to the specifications the size is 6.6”, resolution 1080 x 2400 pixels, RAM 4GB. The emulator says it is “large”, when I run it in the emulator the Toast this time is consistent and says it’s “large”, but then when I run it on the actual physical device it shows as “normal”.
All this seems very confusing!
In practice, I do know that whatever size the Toast choses that is the dimen.xml file that gets applied, so I can work with that. However, I think I may be missing something here, so if anyone has any suggestions as to what is gong on I’d be very grateful to hear them.
TIA, Ian