I want to make the buttons in my activity to change orientation according to its layout size. For tablets, It need to show two buttons in a row with same size, and for small screen mobiles one stretched width button per row. Is it possible with Android ? How can I do it?
-
1Have a look at [this](https://stackoverflow.com/questions/47524010/can-we-make-one-layout-type-for-all-screen-size/47524265#47524265) answer. – Harshad Prajapati Dec 01 '17 at 04:41
-
1create different layout for portrait and landscape mode – Vivek Mishra Dec 01 '17 at 04:43
-
Is there a way without creating multiple layouts ? Can I use constraint layout to do so? – Yesudass Moses Dec 01 '17 at 04:43
-
@YesudassMoses yes, without creating multiple layout..you can do. – Harshad Prajapati Dec 01 '17 at 04:45
-
1See my post it will help you – Raja Dec 01 '17 at 04:57
-
1I don't understand why this question is being down voted. Weird. – Glaucus Dec 01 '17 at 05:07
-
Thanks for all down votes :) :P keep downvoting – Yesudass Moses Dec 01 '17 at 05:16
2 Answers
You have to use in different layout folder in your android resouce folder
like
layout-sw320dp
layout-sw480dp
layout-sw600dp
layout-sw720dp
layout-sw720dp
layout-sw720dp is represented in 10 inch Tab
layout-sw600dp
layout-sw600dp is represented in 7 inch Tab
layout-sw480dp
layout-sw480dp is represented in 4.5 and above inch mobiles
layout-sw320dp
layout-sw320dp is represented in below 4.5 inch mobiles
Note, And also follow your should use different dimens files
Like
values-sw320dp
values-sw480dp
values-sw600dp
values-sw720dp
for more info:
http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html

- 2,775
- 2
- 19
- 31
-
2If you were to go down this path, I would suggest you use layout merging. I would create just one layout for the activity/fragment and then a merge layout just for the buttons container. Then create a bunch of buttons containers to deal with all possible sizes. It's not ideal but better than duplicating a large layout just because of a few buttons. https://developer.android.com/training/improving-layouts/reusing-layouts.html – Glaucus Dec 01 '17 at 04:59
-
1Yes, in this cause also I told, manage with different type of dimens files – Raja Dec 01 '17 at 05:02
You might want to take a look at the Flexbox-layout (made by Google). If you're familiar with CSS Flexbox, you should feel at home with this.
EDIT: Another option, if you're using a RecyclerView, is to use the StaggeredGridLayoutManager. It won't automatically do what you want, however, you can get the screen size and then calculate how many columns you'd like. I'd try the flexbox one first, but I've done something similar with the staggered one.

- 848
- 8
- 14
-
1
-
Can I achieve samething using built-in layouts ? Eg: Constraint Layout ? – Yesudass Moses Dec 01 '17 at 04:52
-
1I just edited my answer to include a second idea. If you intend to have this scroll, using the StaggeredGridLayoutManager with the RecyclerView and dynamically setting the column count could achieve the desired result. Just make sure all items are the same height. – Glaucus Dec 01 '17 at 04:54
-
1To answer your question about the ConstraintView, I'm not sure. However, it seems that it is not possible: https://stackoverflow.com/questions/45699693/can-constraintlayout-help-to-flow-the-right-side-item-to-next-line – Glaucus Dec 01 '17 at 05:02
-
Is FlexBoxLayout heavy ? I want to keep my app slim with minimum 3rd party controls. – Yesudass Moses Dec 01 '17 at 05:07
-
1Not sure, but it obviously will be doing some calculations. I'd suggest it's worth trying. How many buttons are you putting on the screen? My biggest concern is that it appears to be an early version, but this is something made by Google so it's probably not really buggy. You can add it to your project in the same way you'd add constraint layout; in your build.gradle file. – Glaucus Dec 01 '17 at 05:11