-2

i want to create a grid in android wherein suppose

  1. if i pass an arraylist with 5 elements first row should have 3 columns and next row should have 2 columns with centered
  2. if i pass an arraylist with 6 elements first row should have 3 columns and next row should have 3 columns
  3. if i pass an arraylist with 7 elements first row should have 3 columns, next row should have 3 columns and next row should have 1 column with centered

1 Answers1

4

I would suggest you using FlexBoxLayout. It is similar to CSS's FlexBox and fits exactly your needs.

Just add deppendency to Gradle:

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

Put the layout in .xml file:

<com.google.android.flexbox.FlexboxLayout/>

and add view's children dynamically:

flexBox.addView(child);
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90