41

Nexus 7: 7" 1280x800

Galaxy tab 10.1 10" 1280x800

I want my app to run on 7 and 10 inch tablets. As far as I know, I have to include these layout folders in my app:

for 7 inch tablets

  • layout-sw600dp
  • layout-sw600dp-port

for 10 inch tablets

  • layout-sw720dp
  • layout-sw720dp-port

It runs fine on the nexus 7, but loads the sw600dp layouts on the 10" tablet.

If I include these default folders:

  • layout
  • layout-port

10" galaxy tab loads layouts from these.

If I only include the default layout folders and the sw600dp one, it crashes on the nexus7.

How am I supposed to support phones, 7" tablets and 10" tablets, if the 10" galaxy tab won't load the sw720p layouts?

edit:formatting

berestom
  • 1,079
  • 2
  • 10
  • 9

4 Answers4

59

The problem was, that I had no default layout folder.

I tried getting by, using only the sw600dp and sw720dp folders. I still have no idea why they don't work, but I don't care. I can't use swxxxdp <3.2 anyway, so screw that.

So if you want to write an app, that has to support phones(2.2+), 7inch tablets and 10 inch tablets, use the following oldschool stuff:

layout this is the default, it is needed even if you don't plan to support phones!

layout-large for 7" tablet (works on emulator and nexus7)

layout-xlarge for 10" tablet (works on emulator and galaxytab10.1)

Other people have came to the same conclusion too.

Community
  • 1
  • 1
berestom
  • 1,079
  • 2
  • 10
  • 9
  • 1
    I agree, I had same problem, solved adding layout-xlarge and drawable-xlarge folder. – Muhammad Irfan Apr 11 '13 at 06:46
  • I agree with you. sw600dp are't the standard way of implementation anymore . i have not tried your solution actually but i guess that's the only way out. looking for any better ways . i i found one will share . – Abhijit Gujar Feb 22 '16 at 18:04
  • exactly, sw600dp did not work for me. So I used layout-large and layout-xlarge. – TharakaNirmana Nov 24 '17 at 04:49
21

I am also facing such problem in my application. But I found a good solution for this. I have only one layout for tablet and directory name is layout-sw600dp. Now, when part came to height and width problems, I have created several different values directory in which i place dimensions and font size and other stubs. So there will be no constant value in layout of tablet screen.

androd:layout_width:"60dp" // i drop this scenario

androd:layout_width:"@dimen/tab_width" // i used this scenario

and your values directory name will be like

values-xlarge
values-large

All the values will be fetched from your values directory. It will not create different layout, but one layout can be used multiple times.

Following are words of Developer.android site. Configuration examples

To help you target some of your designs for different types of devices, here are some numbers for typical screen widths:

320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

Using the size qualifiers from table 2, your application can switch between your different layout resources for handsets and tablets using any number you want for width and/or height. For example, if 600dp is the smallest available width supported by your tablet layout, you can provide these two sets of layouts:

res/layout/main_activity.xml # For handsets
res/layout-sw600dp/main_activity.xml # For tablets

===

In this, you can see that, layout for 1280*720 is under layout-sw720dp so instead of creating layout-normal-xlarge you should use this thing which lets you to decide differences. Instead of identify differently using layout-large-mdpi and layout-large-ldpi, are't you just identify by its smallest width? Because, android providing drawables directory for different images, only thing is its resolution. And you have above solution.

Edit

Then you must have to develop different layouts. No other option. I found at http://jamil.fluidsoul.net/2011/03/06/creating-android-applications-for-multiple-screen-sizes.

Low density Small screens QVGA 240x320 (120dpi):

layout-small-ldpi (240x320)  
layout-small-land-ldpi (320x240)

Low density Normal screens WVGA400 240x400 (x432) (120dpi):

layout-ldpi  (240 x 400 )
layout-land-ldpi  (400 x 240 )

Medium density Normal screens HVGA 320x480 (160dpi):

layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )

Medium density Large screens HVGA 320x480 (160dpi):

layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)

Galaxy Tab ( 240 dpi ):

layout-large  (600 x 1024) 
layout-large-land  (1024 x 600)

High density Normal screens WVGA800 480x800 (x854) (240 dpi):

layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)

Xoom (medium density large but 1280x800 res) (160 dpi):

layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)
Chintan Rathod
  • 24,674
  • 13
  • 76
  • 92
2

Yes, you should use layout-dependent folders but also make sure any device independent layouts go in your res/layout folder.

This is mentioned on the Android developer site but to reiterate their point, if you have a layout that is only available in an layout-xlarge folder on an app that supports say large and normal sized devices as well, the app will crash as smaller devices will not be able to find any match for this resource.

Here is a good folder structure, start with:

res/layout

Keep a device-independent layout in there, you can avoid this if you are accounting for every possible qualifier type but this is still the safer option.

If you want to add specific layouts for say 7 and 10 inch tablets, use all of the following:

res/layout-large
res/layout-xlarge
res/layout-sw600dp
res/layout-sw720dp
res/layout-sw800dp

And so on for any specific device screen widths you want to support. It should be noted that sw600 supports the smallest possible width, so avoids the use of the screen width when the device is held landscape. Using the swxxxdp qualifers are preferred but these were added in API 13 so you will still need large, xlarge for older Android OS.

Regarding use of the dpi, be aware that if you ONLY set one density qualifier for a size, so layout-large-mdpi for example, then any devices that match the large qualifier will use layouts from here instead of another folder, this is due to the Best Match criteria, which you can read about here. It will match it as a large device before it will match the density so non-mdpi density screens will still use these layouts.

To counter this, you will have to include folders for whatever other densities you support as well, so layout-large-hdpi following on from the above example, and include in this folder hdpi versions of layouts that you have used in the mdpi folder if you require them to be different.

Avoid duplicating your layouts as well of course, don't copy device-independent layouts into every unused folder if you only need them in res/layout, try and only keep the layouts that need these qualifiers in the folders and organise them properly, making sure your folders are named with the qualifiers in the right order of precedence to prevent using the wrong folder for a density/size combination.

sturrockad
  • 3,915
  • 2
  • 16
  • 18
  • does not work. Have gone in and out and searched all around . also other people had the same problem . sw600dp just don't work. for my oneplus one it pulls sw600dp folder layout . – Abhijit Gujar Feb 22 '16 at 18:02
1

This is very strange, since you are doing the correct thing.

The sw600dp qualifier should be selected by the Nexus 7.
If available, the sw720dp qualifier should be selected by the Galaxy Tab.

Are you sure it crashes when trying to find an appropriate layout? Android may find the correct layout, but something in the layout xml file may be missing and the crash is caused by that.

Streets Of Boston
  • 12,186
  • 2
  • 23
  • 27
  • It crashes at the setContentView. Here is another example: ainol venus 7" 1280x800 160dpi won't load the sw600dp layouts. – berestom Feb 26 '13 at 16:45
  • I have tested it again and again. My default layouts would be the 10" ones, they are in the sw720dp folder, and the galaxy tab loads the sw600dp ones – berestom Feb 27 '13 at 09:49
  • @berestom: just for info: ainol venus has sw480dp , 'normal' category of screens (not large neither extra large). – Sergey Vakulenko Mar 14 '13 at 13:54
  • I used this versions and not work correctly, I don't know why, but seems that your layout sw600dp / sw720dp not referenced correctly in tablet landscape –  Jul 14 '15 at 15:17