androidxmlscreen-resolution

Android: distinguish between device resolutions when providing resources


I have and android project providing different dimens.xml for different screen resolutions. I am doing so by defining values-swXXXdp folders in my /res folder.

However, I am now having trouble distinguishing the following two screen resolutions:

  1. 768x1280, 160dpi from
  2. 768x1024, 160dpi

What I tried so far:

Does anyone have a hint, what folder name I have to use, to provide a dimens.xml ONLY for screen resolution number 2?

Thank you in advance!


Solution

  • Does anyone have a hint, what folder name I have to use, to provide a dimens.xml ONLY for screen resolution number 2?

    I don't think it is possible, at least not with w or h qualifiers. Why? According to Providing Resources guide on Android Developers you can specify minimum screen width or height required to load certain resource, but not maximum.

    Lets have a look what happens if you have resources organized like this:

    res/
        values/
        values-h1024dp/
    

    Android has defined behavior of matching resources depending on device configuration. When you open application on device with screen height of 800dp, best match would be values, because 1024dp doesn't fit in 800dp. However if you'll open same app on larger device with screen height with 1024 or higher, it will use values-h1024dp directory.

    Unfortunately you want to use second resources directory only for device of certain dimensions, i.e. 768dpx1024dp.

    Providing resources in Android is designed in different way you'd expect, but has a way to achieve what you need - alias resources.

    To provide resource directories structure where only one directory matches device with configuration w1024dp, I'd create following structure:

    res/
        values/
        values-h1024dp/
        values-h1025dp/
    

    And in values-h1025dp create xml files containing aliases for resources in values. This way you can have one resource file that matches all devices except those with screen height of 1024dp.

    Please note, it will work for devices with configuration you've specified, but only in landscape mode, since I used only h qualifier.