9

I am developing a Calculator App which have a Relative Layout for Portrait Mode. All Buttons and TextViews are placed in this single Relative Layout and a same Relative Layout for Land mode. I have been digging web for almost 15 days but found nothing relevant. My App also have support for Tablets. MinSDKVer is 14 and Target is v21.

Currently I am trying these Layouts folders

1:layout-sw400dp (intended for Note 3 and Mate 7)

2:layout-sw600dp (intended for 7" Tablets)

3:layout-sw720dp (intented for 8.9" and 10" Tablets)

4:layout-small-ldpi

5:layout-normal-ldpi

6:layout-normal-mdpi

7:layout-large-mdpi (intended for 480×800 phones with screen >5" like Pantech Sky Vega Note)

8:layout-normal-hdpi

9:layout-large-hdpi (intended for Galaxy Note 2, Galaxy Note and Galaxy Mega)

10:layout-normal-xhdpi

11:layout-normal-xxhdpi

12:layout-large-xxhdpi(intended for Galaxy Note 4)

13:layout-normal-xxxhdpi(intended for Nexus 6 and for other QFHD Phone)

All these layouts have land mode.

Problem arouse when I placed (layout-sw400dp). Nexus 6, Note 4, Note 2, Galaxy Mega and Large 480×800 phone started using this layout rather than intended layouts.

This is happening because swdp has a higher precedence than layout-normal-xxxx or layout-large-xxxx.

Note 3 and Mate 7 are 400dpi devices that is why I placed layout-sw400dp. Before that all devices were working perfectly except, Note 3 and Mate 7.

And other problem is devices with 540×960 resolution.

I have tried layout-320dp. By using this xhdpi devices and xxhdpi devices started using this layout.

I have also tried these layout for 540×960 But these did not work even after making seprate folder for layout-long-port-800×480.

1:layout-h960dp-w540dp

2:layout-long-port-960×540

3:layout-normal-960×540

I took all screens reading from Android Virtual Device Manager by creating different devices with actual specifications.

I have tried manually setting layout for 540×960 devices in java file and succeeded but I don't want to do that and I don't want to use Linear layout either.

I have thoroughly studied "Android Different Screen Support and Providing Resources".

Please tell the name of all required folders for layout to support all android devices or any other efficient way to do this using java.

I have uploaded all resources files on Mediafire you can Download Calculator Layout.zip

Any suggestions and solutions will be appreciated Thanks in advance

Sorry for English because it is not my native tongue.

Kamil Mahmood
  • 527
  • 9
  • 22
  • 1
    Having so many layouts is a bit overkill. Most of the changes can be achieved with referencing different [dimensions](http://developer.android.com/guide/topics/resources/more-resources.html#Dimension). – Lamorak Mar 19 '15 at 10:52
  • @Lamorak Give me example how you can achieve my desired goal using dimensions because dimensions are nothing but single file containing dimension values that can be referenced from everywhere. If a phone does not pick intended layout then how can I provide correct height and width... I cannot use fill parent or wrap content – Kamil Mahmood Mar 19 '15 at 12:58
  • Usually when you want to fit your layout to different screen sizes, the main thing that changes is the size of the views. Instead of having two similar layouts you can just reference the dimensions. There is not a single dimen file, you can have several of them in folders like `values-normal-hdpi` etc. – Lamorak Mar 19 '15 at 13:04
  • @Lamorak your last suggestion was quite good but don't you think in this case a device can also be able to choose unintended dimensions – Kamil Mahmood Mar 19 '15 at 14:21
  • well at least the chances are equal to choosing an unintended layout but you will avoid a lot of duplicate code and work. Is your calculator layout so complex to worry that much about screen size? – Lamorak Mar 19 '15 at 14:33
  • @Lamorak Layout is simple as it has 22 Square Buttons and Two Text Views whose widths matches parent's width and height is different for every density. It has 36 rectangle buttons and Two text view in Landscape mode. TextViews have same properties as Portrait do have. Problem is this if I have layout-xhdpi then it will not fill the screen in xxxhdpi and buttons go out of view in density – Kamil Mahmood Mar 19 '15 at 14:45
  • so the problem is reduced to keyboard fixed to bottom of the screen and variable space above for two textviews? – Lamorak Mar 19 '15 at 15:07
  • Square(not complete square) keys does not mean they have same height and width for all densities it mean that their shape is square(not complete square) on all densities. Their height and width is variable mean on small screens buttons will be small and on large screen buttons will be large. Keys cover almost 62% of layout in Height and 100% in width and keys are not encapsulated in any other Blocks (I do not know much about this). What if show you my activity_main.xml? You will be able to understand in better way. – Kamil Mahmood Mar 19 '15 at 15:27
  • Yes that would help a lot – Lamorak Mar 19 '15 at 21:07
  • @Lamorak I have edited my question and added Resources files for better understanding have a look again on question. Sorry for late reply because no internet connection was available to me – Kamil Mahmood Mar 20 '15 at 13:29
  • I agree with @Lamorak. This seems like the hard approach to getting things looking good on different devices. – Mark Jul 30 '15 at 18:01
  • You might want to create different drawable folder for hdpi, xhdpi. xxhdpi and so on. For layouts, I would suggest creating folders on the basis of width in dp, like layout-sw320dp. If you face troubles with images in your layour try to create the image files based on guidance on android developer page. – crazy_coder Jul 30 '15 at 18:46
  • @crazy_coder I ended up using Linear Layout because it is really easy to manage and it is consistent between different devices screens. – Kamil Mahmood Aug 01 '15 at 08:59

4 Answers4

4

Instead of using so many simple layout folders, use following folders.

For Smartphones

1.layout:Normal

2.layout-hdpi: use the Nexus one,Nexus S

3.layout-xhdpi : use the Nexus 4

4.layout-xxhdpi : use the Nexus 5

5.layout-xxxhdpi : use the Nexus 6

For Tablets

1.layout-large : use the Nexus 7

2.layout-xlarge: use the Nexus 9

3.layout-xxlarge: use the Nexus 10

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
  • Note: Beginning with Android 3.2 (API level 13), these size groups (large, large, etc) are deprecated in favor of a new technique for managing screen sizes based on the available screen width. If you're developing for Android 3.2 and greater, see Declaring Tablet Layouts for Android 3.2 for more information. ref: [link](https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts) – Andrea Leganza Sep 03 '17 at 22:22
3

Instead of using so many layout folders, use following folders

For smartphones and phablets

  • layout-mdpi
  • layout-hdpi
  • layout-xhdpi
  • layout-xxhdpi

For 7" tablets

  • layout-sw600dp

For 10" tablets

  • layout-sw720dp

If you focus on creating so many layout folders and eventually so many layout files, it will become cumbersome process for every new device/screen specs added into Android family.

Instead, focus on optimizing layouts you are creating to fit in these buckets. Such as providing values in dp and avoid giving hard-coded values for size/margin/padding, use of dimens resource file in efficient way etc.

Hope this will save your some redundent efforts.

silwar
  • 6,470
  • 3
  • 46
  • 66
  • These folders do not cover all devices and you can see these in my question – Kamil Mahmood Oct 09 '15 at 07:20
  • How create **drawable** folder for 8.9' tablets? Now 8.9' tablets takes images from **drawable-sw720dp** , but those images are huge for those tablets. – DaleYY Sep 07 '17 at 10:09
1

I am using Only Three folder layout

- layout-sw600dp (For Tablets).
- layout-sw360dp (For mid size phones).
- layout (for all which has smallest width less than 360).

Rahul Jain
  • 276
  • 2
  • 11
0

For Different screen size, The following is a list of resource folders in an application that provides different layout designs for different screen sizes for small, medium, high, and extra high density screens.

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

The following code in the Manifest supports all dpis.

<supports-screens android:smallScreens="true" 
          android:normalScreens="true" 
          android:largeScreens="true"
          android:xlargeScreens="true"
          android:anyDensity="true" />

For more see here

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96