javaandroidscreen-sizeandroid-screen-support

How to support different screen sizes with smallest width?


I looked up how to support screen sizes but the android guide about screen size support is really vague in explaining.

So my question is how do i support different screen sizes with the smallest width qualifier?

I have dimens.xml files with values sw320dp, sw480dp, sw600dp and sw720dp.

320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc). 480dp: a large phone screen ~5" (480x800 mdpi). 600dp: a 7” tablet (600x1024 mdpi). 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

This is stated in the android documentation, but when i try an emulator Nexus one 3.7 inch hdpi it uses the same dimens value as a Nexus 5 which is 5.0 inch xxhdpi.

Which is the recommended way to support all screensizes?

Thanks


Solution

  • The Nexus 5 has a screen size of 360x640 dp. The largest value for smallest width that is not greater than 360 in your set of definitions is sw320dp. That's why both emulators use the same values.

    However, this is mostly beside the point. The right way to support all screen sizes is to create layouts that can stretch and shrink to fill any phone. Of course there are cases where it might make sense to define different dimensions for different screen sizes, but you can do quite a lot by simply using LinearLayout with weights or ConstraintLayout with match constraints.

    In my own professional experience, the most common reason to use swXXXdp was to create different base layouts for phones and tablets. That is, every activity had a layout XML file in res/layout/ but some also had a tablet-specific layout file in res/layout-sw600dp/. It was very uncommon to define dimensions based on the device size.