0

Hi I have created a gridlayout with columns of 4

but I have 7 which 3 at bottom I want to center

enter image description here

I want to make my gridlayout something like this.

enter image description here

how can I achieve it?

my gridlayout is this

    <GridLayout
        android:layout_width="242dp"
        android:layout_height="fill_parent"
        android:id="@+id/gridLayout"
        android:columnCount="4"
        android:layout_gravity="center"
        android:stretchMode="columnWidth">

// appending

    for(int x = 0; x < something.length(); x++) {
        View view = to do///
        gridLayout.addView(view);
    }
Beginner
  • 4,118
  • 3
  • 17
  • 26

2 Answers2

0

If you are using Recyclerview with GridLayout then there are one option to acheive this:

<android.support.v7.widget.RecyclerView
android:layout_gravity="center"/>

And don't forget to give Padding in rawfile main layout or else all cell comes into center but not space between them.

SahdevRajput74
  • 754
  • 7
  • 18
-1

I found a solution thanks to this https://stackoverflow.com/a/34735650/3481654

I use nested linear layout instead

int cols = 4; // no of columns display per row

Linear llItems = // linear layout container

LinearLayout layout = createLinearLayout();
llItems.addView(layout);

for(int x = 0; x < bonusInfo.length(); x++) {

    // create linear layout container to align
    if (x != 0 && x % cols == 0) {
        layout = createLinearLayout();
        llItems.addView(layout);
    }

    if (layout != null) {
        //insert items in created rows
        View item = inflater.inflate(layoutId, null);
        layout.addView(item);
    }
}
Beginner
  • 4,118
  • 3
  • 17
  • 26