Search code examples
androidlayoutdimension

Android device width in dp


I have a lack of experience in developing android apps. I've decided to separate up dimensions.xml into 4 configuration qualifiers : default, sw480dp, sw600dp, sw800dp. Where can I see the quantity of android devices that have certain screen width in dp?


Solution

  • There is no way to know how many devices have certain display aspects, you just have to support it, because it is possible to exist at least one device with that aspects.

    Check this article, here you can find some informations you may looking for.

    EDIT

    You want to support multiple screen sizes, you could do as the following (I consider this the best aproach, I use it, it is very intuitive and I never get any colateral results, if anyone think this is not good, please let me know):

    • Your project contais a folder called values, inside it create a file called dimens.xml (can be anything, the name doesn't matters, it is just a convention) and then, inside, create something like this:

    <dimen name="my_dimen">32dp</dimen>

    • 32dp will be your default dimen for all screen sizes. Here is an example for how to use it:

    android:layout_width="@dimen/my_dimen"

    • Lets say you use the dimen you declared to represent some img size, 32dp may be good for small screens, but could be too tiny for bigger screens, you can create a separate file that will have different dimen values depending on the size of the user's screen, then the Android OS system will pick the best for you. Go to the res folder (the same folder where values folder is present) and than create a folder called values-hdpi, values-xhdpi, values-xxhdpi and so on.

    • Inside each folder, create the same dimens.xml file and on each file create the same <dimen name="my_dimen">xxdp</dimen>, where xx could be 40, 50, 100, 1000, you can set any value that could fit your requirements.

    Here is an working example:

    values >> dimens.xml

    <dimen name="margin_big">21dp</dimen>
    

    values-hdpi >> dimens.xml

    <dimen name="margin_big">28dp</dimen>
    

    values-xhdpi >> dimens.xml

    <dimen name="margin_big">40dp</dimen>
    

    ...

    How to use it:

    <!-- rest of code -->
    android:layout_margin="@dimen/margin_big"
    <!-- rest of code -->
    

    How this will work:

    The article I posted tell us something about what is mdpi, hdpi, etc, lets say the Android determine some screen as hdpi, the OS will go to the values-hdpi and will look for a dimen called margin_big, he found it? Take its value and do whatever he have to do with it. OMG THERE IS NO margin_big INSIDE VALUES-XHDPI (lets imagine this is true), no problem, forget values-hdpi and go for values folder, this folders have to contains all default datas idependently of the screen size, if anything goes wrong android will look here for what he wants.

    PLUS

    values-pt = values for portuguese devices. values-jp = values for japonese devices and so on.

    See this article for more information.