1

I am getting very confused on how to support all different Android screen sizes. I have already checked this answer, this site, the official documentations and many other answers but still I am confused so please don't mark this as Duplicate.

I already created layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, layout-sw480dp (I only need to support up to 6 inches so I don't create layout-sw600dp).

What do I have to do is just have one layout and just copy it in each different folder so Android will do the selection on its own or I also need to change values in each layout?

Community
  • 1
  • 1
Johny
  • 625
  • 2
  • 6
  • 26

3 Answers3

0

What do I have to do is just have one layout and just copy it in each different folder or I also need to change values in each layout?

You start by deleting the layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, and layout-sw480dp directories that you created. Or, at least, ignore them for now.

You then start implementing your UI, putting layout resources in res/layout/.

Then, for those layouts that need to be different in certain scenarios, you then create a directory representing the size when you want a different layout. Copy the layout from res/layout/ into the new directory, and then modify that copy to reflect whatever changes you want.

In other words, one copy of every layout is in res/layout/, and you override where needed with additional, modified copies in specific directories where you need the UI to change.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So what you are describing now is to remove all the different folders I have and only have the default `layout` folder? – Johny Apr 20 '17 at 15:24
  • @Johny: I am saying that you *start* with just the `res/layout/` directory. You only add other directories *if and when you need changes*, and you only create the directories and the revised layouts that reflect those changes. Some of your layouts will only ever need the one copy in `res/layout/`. Others may need altered versions in some other directories. – CommonsWare Apr 20 '17 at 15:31
  • So I create directories (layout-sw120dp etc) and just put the different layout in each different folder if needed, otherwise I don't need to create them. – Johny Apr 20 '17 at 15:34
  • 1
    @Johny: Correct. Most apps do not need very many additional layout directories, because usually we can create layout resources that handle small changes in screen size well. In your case... if you are focused on 6" and smaller screen sizes, you should be able to do everything just in `res/layout/`. *Maybe* a few of your layouts might need a different version for screen sizes at the high end of your range. Web sites, for example, usually do not have different CSS rules for 3", 4", 5", and 6" screens, as the one design can handle all of those. – CommonsWare Apr 20 '17 at 15:39
  • I am not sure if I am able to do this but can you check also my other question as well? http://stackoverflow.com/questions/43536521/support-different-video-sizes-for-android – Johny Apr 21 '17 at 10:14
0

This is an answer that's been an issue from old ages and for which you'll see lot many answers but which is not a one fit all type still. What I did come up with though when faced with the same issue was to use PercentRelativeLayout. This is a relatively new feature that was started from Android support version 23.0 (android sdk manager), and one of the big game changers according to me, since it allowed developers to specify their blocks relative to the layout size Percentage-wise. And since it is in terms of percent, this fits all screen sizes with varying dimensions, but while maintaining the ratio.

Ofcourse this method involves some trial and error and a lot of experimenting, but I found this method to be the easiest to implement and which took out the headache of maintaining the dimensions for various screen sizes.


Few tutorials to get you started :

https://www.tutorialspoint.com/android/android_relative_layout.htm https://guides.codepath.com/android/Constructing-View-Layouts

Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
  • If it supports version 23.0 then this will not help because I need to support from version 16.0 – Johny Apr 20 '17 at 15:29
  • PercentRelativeLayout comes from a support library, so it doesn't matter what API you have. So yes, API 16 will work. What I meant by support version 23 is that your Android Support Library in SDK Manager should be 23.0 or above. – Kaushik NP Apr 20 '17 at 15:34
0

If you want to use the same layout for each and every screen density, you don't need to create different folders. Use just one simply called "layout" and the system will interpret it for every density. However, this could lead to strange layouts on certain physical devices depending on their screen size and density... Another point you have to be aware of, if your application supports orientation changes, is that you have to design layouts for portrait and lanscape orientations. This is done by duplicating a folder used for a density and add "-port" or "-land" to inform the systen which one must be used according to the actual orientation of the device your app is currently running on. If you want to precisely define your app look and feel, you have to customize your layout for each density. And if you use bitmaps, you will have to customize them either (for example, your app icon should be defined with different sizes to keep a good looking for each screen density). Just as for the layout, you have to create "drawable-..." folders to contain the corresponding versions of your bitmaps.

Zelig63
  • 1,592
  • 1
  • 23
  • 40
  • I only need to use portrait so there is no any issue. I already have `drawable` folder for each different image and `raw` folder for each different video. But have only one `layout` folder how I will manage each different screen size and density? – Johny Apr 20 '17 at 15:27
  • If only one layout folder doesn't suit your needs, you have to customize the layouts you put in each "layout-..." folder and arrange the elements in order to get the layout you need for any density and screen size. There is no easy way to do this, but Android Studio, for example, helps you in this task by allowing you to preview the result on many different screens size and density. – Zelig63 Apr 20 '17 at 18:33