Search code examples
androidandroid-layoutscreen-densitydensity-independent-pixel

How can i support different measurements for screens in android


I basically want to accomplish an adequate size of my layouts depending on the screen size of the device. I am using dp as unit, but it doesn't seem to work. How can i do that?


Solution

  • For sure dp is the most adequat unit to accomplish that goal. There in addition to that, you have to define several values folders where you define dimensions for scaling tablets or phones. E.g. define two values folders like:

    values-large values-xlarge

    Now define a dimen.xml file inside this folders, where you can put your measurements in (unit dp) for the corresponding screensize. Define a measurement like this:

    17dp

    Then embedd this sizes in your layout xml, like:

    android:layout_height="@dimen/value1"

    Depending on the screensize, the system will load the correct measurements from this folders, e.g. if you have a screen size large only the defined values in folder values-large will be loaded. For more information, also have a look at https://developer.android.com/distribute/essentials/quality/tablets.html