1

I want to make a responsive design in Android i.e. I need such a layout in which I will add views programmatically and they will adjust them according to screen size. For example: I want to add 10 views in a layout When I add them if there is place for only three views per line then automatically they get adjust in four rows and three columns i.e

[0,0] [0,1] [0,2]

[1,0] [1,1] [1,2]

[2,0] [2,1] [2,2]

[3,0]

But as in landscape mode, there is more space vertically

[0,0] [0,1] [0,2] [0,3] [0,4]

[1,0] [1,1] [1,2] [1,3] [1,4]

Shaifali Rajput
  • 1,279
  • 12
  • 30
Faiq
  • 31
  • 9

2 Answers2

1

Use flexbox layout

In builde.gradle add

dependencies {
...
compile 'com.google.android:flexbox:0.3.0'
}

and use it in the layout xml. i.e.

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:alignItems="center"
    app:alignContent="center"
    app:flexDirection="row"
    app:flexWrap="wrap">

   ...
</com.google.android.flexbox.FlexboxLayout>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anemomylos
  • 546
  • 1
  • 6
  • 14
1

It seems the Flexbox solution worked for you, however there is another approach that can provide a different avenue for this problem. In case you are not aware, you can actually define different XML layout files with the same name - 1 for portrait mode, and another for landscape mode.

When creating a new layout file, if you right click on the layout directory and select New > Layout resource file you can configure the orientation here. In the Available qualifiers section, scroll down to Orientation and add it to the file. At this point, you will be able to choose the orientation for this layout and, if you name the file the same name as the opposite orientation, your app will automatically inflate the view that is appropriate for the device's current orientation. Android Studio will also understand the different layout orientations you have chosen while in the editor, and the screen you see in the Design tab will be adjusted accordingly i.e. the landscape layout file will have more room vertically to build the UI with.

Note that this adds a level of complexity to the application itself in the sense that the activity lifecycles will need to be accounted for since the views are now being destroyed and re-drawn by the OS in response to when the user switches the orientation of their device.

Hope this helps either in this situation or in some other situation where you want to accomplish different layouts based on device orientation!

DMP
  • 129
  • 4
  • Thanks for this solution! BTW I guess in flexlayout maybe views get destroyed automatically? – Faiq Sep 05 '17 at 20:17
  • Never worked with the flex layout, so I couldn't tell you for sure. Likely that is gets destroyed since the UI changes - I would think it would follow the rest of the Android architecture @Faiq – DMP Sep 06 '17 at 12:23