1

I have an app with about 20 xml layout files. Altough having spent quite some time to make a generic constraint layout for all devices (which was not possible for me), I now want to create an individual layout file for phones, small tablets and big tabelts. So I will have have 20 layout files for phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).

My question is, where shall I store them such that Android automatically picks the correct layout depending on the used device? In this question Layout for tablets in Android it is said that I should store the xml layout files in

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

or

res/layout/my_layout.xml            // layout for normal screen size
res/layout-small/my_layout.xml      // layout for small screen size
res/layout-large/my_layout.xml      // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mod

However, I can't distinguish the devices by their pixel density because different devices with the same display size can have different resolutions. Further, I would like to make the 20 xml layout files not for one specific size but for ranges of display sizes (otherwise I'd have to create too many xml layout files).

How can I define folders for storing xml layout files for ranges of display sizes: phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).

Reminder: I'll appreciate every furher comment as I still don't know what to do

VanessaF
  • 515
  • 11
  • 36
  • 1
    instead of 3 layout use this https://github.com/intuit/sdp – Dhruv Sakariya Dec 10 '22 at 09:28
  • Do you use `dps` on your layout dimensions? If you did so, you don't have to worry about the screen densities. – Zain Dec 16 '22 at 12:44
  • @Zain: Thanks for your comment. What do you mean by dps on your layout dimensions? I use sdp from this package https://github.com/intuit/sdp. Still, I need to have different layout files for different displays as the layout of a tablet is different from the one of a small smartphone. It was not possible to create one layout that fits alls sizes. So I need to have 3 layouts for each XML layout file for display sizes: phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display). – VanessaF Dec 16 '22 at 16:18
  • Just `dp` i.e., Device Independent pixels; they will select the appropriate number of needed pixels per each device density; so the views would be stretched/shrinked automatically. I didn't give this library a try – Zain Dec 16 '22 at 18:09
  • @Zain: Thanks Zain for your comments. I have to create 3 different versions of a layout file for 3 different device types. Using dp does not help at all, because the layout of a tablet looks different compared to a phone. So dp is not the solution. My question is how can I define folders for storing xml layout files for ranges of display sizes: phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display). Do you have any ideas – VanessaF Dec 17 '22 at 08:46
  • Please have a look at [window size classes](https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes#window_size_classes); this would make you do that programmatically by checking the width & height of the screen. – Zain Dec 17 '22 at 12:31
  • Is there no way of defining different layout versions for differnet screens in Android? As already said, I spent a lot of time trying to define one layout that fits all screens also programatically, but I could not manage to do this. Thus, I would like to have 3 different XML layout files for one fragment. Is this not possible – VanessaF Dec 17 '22 at 12:54
  • @Zain: Thanks for your comment. Any comment to my last comment. I'll highly appreciate every further comment from you. – VanessaF Dec 18 '22 at 18:18
  • I don't think there is a single solution that works for every layout; but would recommend to go for the responsive solution using ConstraintLayout, stick to constrains and try to avoid hardcoding the dimensions as much as you can. – Zain Dec 18 '22 at 19:41
  • @Zain: Thanks, but in Android it is possible to store layouts for different screen sizes and desities. Why it is not possible to define different layouts for different devices based on their display size? – VanessaF Dec 18 '22 at 20:04
  • @VanessaF It's possible to store layouts based on screen size; AFAIK and told before densities is controlled using `dp` measures; good luck :) – Zain Dec 18 '22 at 20:17
  • @Zain: Thanks for your answer Zain. I don't understand your comment. What do you mean by "densities is controlled using dp measures"? What I want is just to have one layout file for 3 different screen sizes. Each of the three layouts will have different `dp` (or in my case `sdp`) values. How can I store these 3 layouts such that Android knows which one to choose depending on the currently used device? – VanessaF Dec 18 '22 at 21:29

3 Answers3

1

For your question and anybody with same issue,

  • Use Constraint layout to make flexible UIs and use qualifiers.
  • You will have to have multiple layout files to better support all screen sizes and orientations,
  • but no need to make those folders yourself - make a default xml layout for a small mobile screen first, then make an xml layout file with same name as the default one but add a qualifier sw-600dp(or as per use case) to it from 'Available qualifiers' list and adjust the layout. then make one with sw-700dp and adjust it.
  • Android will automatically detect which layout file to use based on the qualifiers provided.
  • For reference, please see this example from docs. Quoting from which Android follows an order of precedence when determining which resources to apply. This Order is the same in which qualifiers are given in 'Available qualifiers' list to choose from.
  • Thanks Vipul for your answer. But how can I decide about the number in`sw-600dp`? I would like to have 3 layouts for 3 devices (Phone, Small Tablet, Big Tablet). How do I know this number? – VanessaF Dec 20 '22 at 21:23
  • Take a look at this link (https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes#smallest-width) , generally 360 dp is for small mobile phones, 420 for large screen phones(most phones now a days), 600 small tabs, and so on. You can put any value though like 380 or 400, its not a rule use these values, but its a standard practice. – VipulSharma Dec 21 '22 at 06:42
  • Thanks Vipul for your answer. How do I determine the dp of a device? – VanessaF Dec 21 '22 at 21:39
  • There ways to know the precise dps of a device at runtime [see this](https://stackoverflow.com/questions/6465680/how-to-determine-the-screen-width-in-terms-of-dp-or-dip-at-runtime-in-android). I don't know how to know it before hand. Though using approximate values can cover most cases – VipulSharma Dec 22 '22 at 05:41
  • Thanks Vipul for your comment. In your answer you write "add a qualifier sw-600dp". Where exactly do I have to add them? Can you make an example for me? Do I have to create three different folders with the qualifieres? – VanessaF Dec 22 '22 at 08:21
  • A qualifier can be added while creating and giving name to your xml file in android studio, the option is available in that same window. No need to create folders yourself. I hope it helps – VipulSharma Dec 22 '22 at 12:55
0

Use this dependency.

implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.intuit.sdp:sdp-android:1.0.5'

Then apply

android:layout_width="@dimen/_24sdp"
android:layout_height="@dimen/_24sdp"
Dhruv Sakariya
  • 792
  • 4
  • 10
  • Thanks Dhruv for your comment. Acutally I am using the framework you posted with sdp and ssp. Still, it was not possible for me to make a generic layout for multiple devices as the devices are too different. So I need to have 3 different versions of 1 layout file altough still continuing to use sdp and ssp. The framework is good for scaling within the clusters of devices phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display) but not for scaling across the clusters of devices – VanessaF Dec 10 '22 at 09:35
  • Thanks for your comment. Any comments to my last comment? I'll highly appreciate every furhter comment from you – VanessaF Dec 17 '22 at 08:53
  • then i think you should make three different layout file with different sizes and conditionally inflate according to screen sizes. – Dhruv Sakariya Dec 19 '22 at 04:56
0

First of all, you wrote down that you've decided to apply changes in the display based on inch sizes, this is not suggested. As there are many different digital gadgets with varying pixels in each inch of the display screen, which means just having the upper production dimension doesn't result in better resolution quality; Android provided a unit to solve the problem, it is a ratio: dots per inch "dp". Manage a time to look at Android's official guidance on how measuring works:

measuring

Here you can find suitable size dividers & definitions of why are they declared.

To give your App the ability to choose an appropriate layout to display, you must add new Activity layouts or Fragment layouts in directories based on resolution. Just create a layout with the same name as the currently existing one, but change its directory to the desired size category e.g.: layout sw-600dp, layout sw-840dp, etc. Then design the created layout to be responsible for the mentioned display.

This is how your res may look:

This is how your res may look:

I could provide more clear suggestions if you could express essential parts of your code.

At last, if your App is just Handled by Activities you may need to use fragment a tool that can hold & show different layouts.

Take a look at this

  • Thanks Hamed for your answer. Actually I have a lot of fragments (about 20) that I need to convert now to 3 different layout files each. I used the contraint layout and sdp instead of dp because dp is not really good if you have different device cizes. dp just does not scale with the screen size, this is why I personally don't see dp as a good solution if you want to create a layout for differenz screen sizes because for me it's not really device indepandant – VanessaF Dec 22 '22 at 21:05
  • Please, use link formatting: `[link](url)` – SerjantArbuz Dec 23 '22 at 13:02
  • Edited, link formatting used. – Hamed Mohamadi Dec 24 '22 at 21:12