-4

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?

Android Responsive Layout

Yesudass Moses
  • 1,841
  • 3
  • 27
  • 63

2 Answers2

2

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

Different values folders in android

Raja
  • 2,775
  • 2
  • 19
  • 31
  • 2
    If 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
  • 1
    Yes, in this cause also I told, manage with different type of dimens files – Raja Dec 01 '17 at 05:02
1

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.

Glaucus
  • 848
  • 8
  • 14
  • 1
    Why the down vote? It seems like a great use case for it. – Glaucus Dec 01 '17 at 04:48
  • Can I achieve samething using built-in layouts ? Eg: Constraint Layout ? – Yesudass Moses Dec 01 '17 at 04:52
  • 1
    I 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
  • 1
    To 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
  • 1
    Not 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