1

I have made several values folder and dimen.xml files for configuring my app for different screen sizes. The preview in Android Studio shows them perfectly however, when I run the app on the phone, those dimensions don't take effect. And the layout shown in the Android Studio layout and the layout shown on the phone are different.

So for example on my nexus 5 the app should read dimensions from values-w640dp/dimen.xml however, it reads them from values/dimen.xml for some odd reason.

What can I do to resolve it?

Here are the screenshots.

Android Studio Preview Snapshot (Nexus 5)

enter image description here

Nexus 5 Phone Screenshot:

enter image description here

Edric
  • 24,639
  • 13
  • 81
  • 91
user2498079
  • 2,872
  • 8
  • 32
  • 60

1 Answers1

1

So dp are calculated using the following formula:

pixels / dp = dpi / 160dpi or pixels / dp = density

Looking at device metrics, the Nexus 5 has a density of 3 so:

dp = pixels / density
dp = 1920px / 3 = 640dp
dp = 1080px / 3 = 360dp

Using values-sw600dp would not fix it because that is not the actual smallest width of the screen since it can also be 360. So you should use values-sw300dp. Be warned though that this covers a very wide variety of devices. values-sw600dp usually targets tablets.

I have also run into issues where the true resolution of the device does not match what the emulator created, with the Nexus 5 too, so make sure you check the AVD screen and ensure that the resolution matches the real resolution of the device. Example, the Nexus 5 should be 1920x1080 and it is. If you ever encounter this issue, recreate the emulator or try a different one entirely.

enter image description here

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • You mean values-sw300dp? How can I design the layout for all the major screen sizes? – user2498079 Jun 16 '16 at 12:56
  • I am not running the app on emulator. I am running it on real device – user2498079 Jun 16 '16 at 13:01
  • Yes, values-sw300dp, I corrected that, thank you. What exactly is your concern with your layout? Most standard layouts cover phones pretty well, people only run into issues with a lot of white space in tablets. If your concern is scalability of components, like pictures, you can look at the density of the device as a good starting to make decisions. In general though, that's a concern for specific components of a layout, pictures in particular. Most other components will scale well. – Daniel Jun 16 '16 at 13:02
  • What are the major sw<>dp sizes that I should cover in order to make my app's layout looking valid and proper. – user2498079 Jun 16 '16 at 13:09
  • There is no perfect answer so I'll give you a couple of rules of thumb. **sw600dp** is a nice break point between tablets and phones so you should consider making a different layout for **sw600dp**. Most Android components will look okay regardless of the size of the device. Resources, like pictures, is where you'll run into problems. So the best solution is to have an appropriately sized resource for each screen density, so one resource for **mdpi**, one resource for **hpdi**, and so on. Android will then pull the appropriate resource based on the device's density. – Daniel Jun 16 '16 at 13:18