43

I am creating different values folders in my app (values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi, values-nodpi, values-w360dp-mdpi). But some devices that belong same category. But having different screen sizes. But I see give font size according to device densities in this the answer provided by @PankajSharma suggest to create folders like-

res/values/dimens.xml    
res/values-small/dimens.xml    
res/values-normal/dimens.xml    
res/values-xlarge/dimens.xml

I want to know what is the difference b/w my way and the other way? I think the answer provided by @PankajSharma is easy. I also want to know which way is better?

Community
  • 1
  • 1
John R
  • 2,078
  • 8
  • 35
  • 58
  • I think you should take a look at http://developer.android.com/guide/practices/screens_support.html there you can find why is like that. And you can see a example here http://developer.android.com/training/multiscreen/screensizes.html – PedroAGSantos Jan 22 '14 at 10:30
  • I see these links before but not clear. – John R Jan 22 '14 at 10:50
  • and this one helps ??http://stackoverflow.com/questions/13068273/drawable-folders-for-different-screen-size-and-density-combinations – PedroAGSantos Jan 22 '14 at 10:56
  • But my question about values folder not about layout folders. – John R Jan 22 '14 at 11:08
  • 3
    Th answer is the same the diference between small and dpi is small is related to screen size not to screen density. You can have a small screen with hi density. something like values-small-hdpi. that's the diference between small and ldpi. Imagine a table with a bad screen like really low resolution folder is values-xlarge-ldpi but you can just use values-ldpi and it will work. Hope I have halped. – PedroAGSantos Jan 22 '14 at 11:19
  • plz refer this link for answer http://stackoverflow.com/a/24401563/1554935 – Ritesh Gune Jun 26 '14 at 11:22

2 Answers2

78

The approach you are using is a valid approach, but a little outdated. From HoneyComb, there is a new way to fix all of this. Your resources folder should now look like this:

enter image description here

Please refer to the link I have posted and familiarize yourself with Smallest Width concept.

Hope this helps :)

EDIT: Adding to this post, try to establish some kind of standardization in your dimens.xml, something like this:

enter image description here

Doing this makes it easier to maintain code, plus it reduces the number of dimen folders. Normally rather than having values-hdpi, values-xhdpi, etc. files like values-sw480dp-xhdpi might have more values to adjust, but then again all of this is contextual.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • sw320dp means minimum width 320dp? – John R Jan 24 '14 at 07:17
  • 3
    Why do we need to append -hdpi, -xhdpi for same sw480dp for ex? – Braj Jul 22 '14 at 08:43
  • 1
    Because these devices are further categorized into such measurements. – Rakeeb Rajbhandari Jul 22 '14 at 08:47
  • @user2247689 Ok. Then there will be hell lot of folders if u want to support for all existing devices :( – Braj Jul 22 '14 at 08:51
  • You could see how applications like facebook, etc handle this. See the res files from the apk. – Rakeeb Rajbhandari Jul 22 '14 at 09:03
  • Is it work `values-ldpi`, `values-hdpi`, `values-mdpi`? – Pratik Butani Feb 28 '15 at 05:49
  • rather than using values-ldpi, better to use values-normal-ldpi, because values-ldpi wasn't working in my case, but values-normal-ldpi worked. Specifically for micromax bolt device – Shirish Herwade Mar 04 '15 at 14:14
  • I don't understand the need to further categorize `values` in `hdpi` and `xhdpi` if you're working with `dp`. Care you explain please? – Daniel Dec 18 '15 at 17:42
  • @Daniel We are working in dp but for devices that fall in different categories, in this case 'hdpi' or 'xhdpi', the default dp measurement may not scale well. The values may need to be scaled up or scaled down according to how much pixels you want your assets to occupy. (px = dp* (dpi/160)) I hope this helps. – Rakeeb Rajbhandari Dec 20 '15 at 07:03
  • @RakeebRajbhandari shouldn't there be "values-sw480dp-hdpi" .xhdpi and xxhdpi and so on ? only 320dp have such in your answer.. please reply – Sharp Edge Jul 20 '16 at 16:50
  • how to you get the values for dimens in dp? it is mystery for me. cause all dp should seen same in all device but you changed the dp size for example padding_small=10dp and padding_normal=16dp – Mahdi Aug 28 '16 at 05:22
  • 1
    making a good design will decrease the number of values inserted in the different dimen files and combining this with AutoFitTextView library will make the different dimen files to include only paddings, spaces, radiuses and etc – Sir NIkolay Cesar The First Nov 21 '16 at 08:08
  • is there a big difference if we only create values-sw320dp, values-sw600dp...et cetera instead of values-sw320dp-xhdpi, values-sw320dp-hdpi, values-sw320dp-mdpi? Is that suffix mdpi/hdpi/xhdpi at the end of values-swXXXdp that important? – Red M Jul 19 '18 at 16:12
16

Create a Single layout for default screens 4.7 inch (hdpi) in layout folder and dimensions in values folder. This is your Superset.

Now let say you want your layouts for 7inch devices. Create values-sw600dp folder for 7inch in Portrait orientation

Now lets say you want your layouts for 10 inch devices Create values-dw720dp folder

NOTE :- For landscape just add "-land" in front of folder names.

Now lets say you have new devices such as Xperia SP (4.7' and XHDPI) and Nexus 5(5" and XXHDPI).

For these, you can create values-xhdpi and values-xxhdpi folders and similary add -land for landscape orientation..

I hope you got the point of how to create folders..

Now your superset is defined in values folder. Most of the dimensions will be used from here only. Now run your app in other devices. Whatever mismatch is occuring just add that specific dimension in their respective values folder

To check from which folder your layouts, images are used, use my trick.

Create five same strings and put in it all the values folders like this :- Default Screen Screen 4.7 XHDPI Screen MDPI Screen

Create five drawable folders, most of them will be already there : - drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi Put the screenshots below in their respective folder under the same name

enter image description here enter image description here enter image description here enter image description here enter image description here

This is how my res folder looks like and i am supporting all the devices from 4.7 screen and above :-

enter image description here

Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
  • 7
    Are you always copy&pasting your own answer across questions? How about commenting with a link to your previous answer instead of repeating and extending it (a bit). But the best part: You fix an issue with your question here but not on your previous answer... great! – WarrenFaith Jan 24 '14 at 16:19
  • 7
    "Create a Single layout for default screens 4.7 inch (hdpi) in layout folder and dimensions in values folder." This is wrong. The screen size has no direct relation with the dpi. – WarrenFaith Jan 24 '14 at 16:26
  • Don't use eclipse – Vodet Dec 06 '17 at 10:12