Home » Android » How to cenralize last uneven row in GridView in android?

How to cenralize last uneven row in GridView in android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a GridView , which displays pictures of alphabets
what happens is , every time in the last row there are less no of alphabets.
and the last row is left aligned , which doesn’t look good
so I want make the last row to be in centre,
any suggestion

My GridView code:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="70dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="20dp"
    android:gravity="center"
    android:stretchMode="spacingWidthUniform" >  

</GridView>

enter image description here

enter image description here

How to&Answers:

I have the same problem, but I managed this using two gridviews, with first gridview displaying the rows except the last row, with the second gridview displaying the last row. This is the simplest way to achieve what I need.
I have not find a better solution. Looking forward to see a nice and simple way also.

Answer:

you could try using invisible elements, it not the most efficient solution but is simple, you can put two elements, one at the start, one at the end of the row and make them invisible

Answer:

For those who are still looking for a solution for this here’s my take for this, create a TableLayout and, and for each TableRow set its weigth to android:weightSum="4" and dont forget to add android:gravity="center"

For complete code check this gist for reference.

Answer:

You could center the grid row inside of a relative layout and then add android:layout_centerHorizontal="true" to the alphabet ImageView‘s. Assuming that they’re ImageView‘s.

So your GridView would be wrapped in something like this:

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_cotent"
    android:orientation="horizontal" >

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="70dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="20dp"
        android:gravity="center"
        android:stretchMode="spacingWidthUniform" >  

    </GridView>
</RelativeLayout>

And the individual alphabet images would need android:layout_centerHorizontal="true" like this:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
</ImageView>