0

I read this official link because i want to set different XML for larger screen.

First, i checked my device's width (in dp) with this code :

private void getScreenDp() {
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
}

The width is 320dp.

Then i create a new folder called layout-sw320dp. When i run the app in 320dp width device, its using layout-sw320dp correctly.

However, the layout that my apps used when running in 600dp width device is the one in layout-sw320dp instead of the normal layout folder. I tried to change the layout to layout-w320dp but the devices bigger than 320dp width still not using the normal layout as it should be.

Please kindly help me, i have spent hours for this.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • 2
    [Here](http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts) they say: `Note: Remember that all the figures used with these new size APIs are density-indpendent pixel (dp) values and your layout dimensions should also always be defined using dp units`. And they define the dp [here](http://developer.android.com/guide/practices/screens_support.html#terms): `The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160).` - I hope it helps. – Phantômaxx Jan 11 '15 at 08:55
  • 1
    @DerGolem Thanks. Do you mean the code i used to count the `dp` is wrong? – Blaze Tama Jan 11 '15 at 12:03
  • 1
    Well, suppose that your tablet width is 1024 px (in landscape orientation). With a density of 320 dpi. Then the dp is `dp = px / (dpi / 160)` => `dp = 1024 / (320 / 160)` => `dp = 1024 / 2` => **dp = 512** – Phantômaxx Jan 11 '15 at 12:22
  • 1
    @DerGolem Thanks a lot, i will try to re-count the `dp` tommorow. Hope its working :D – Blaze Tama Jan 11 '15 at 12:25
  • Don't forget that you have to provide another folder for the portrait mode. Supposing the with in portrait mode is 768 px, then the dp is **384** (for a screen density of 320 dpi). – Phantômaxx Jan 11 '15 at 12:27
  • This is how to get the device density (other than getting it from the manufacturer site product specifications): http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android – Phantômaxx Jan 11 '15 at 12:32

0 Answers0