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
CodePudding user response:
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"
CodePudding user response:
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 withsw-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.