How to cenralize last uneven row in GridView in android?
Asked Answered
R

4

9

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

Rimrock answered 3/10, 2013 at 10:7 Comment(9)
Wrap the current adapter that you set on the GridView with another adapter that will insert some extra dummy elements(if needed) so your last items will be centered.Houseraising
@Luksprog : but no of columns depends on screen size , how can i calculate how many more elements to add moreGavingavini
I think you could use a ViewTreeObserver and see in there how many columns the GridView is using(getNumColumns()) and then trigger an adapter change(adding elements if needed) based on that value. Be careful to not get yourself in an endless loop.Houseraising
@HussainAkhtarWahid'Ghouri' : Did you find any perfect solution for this. I also need to implement the same type of layout in my project. Can you share your layout code with me? ThanksValuate
@Valuate : I had this static requirement , So I went for Table View , works for meGavingavini
Hmmm, I need to implement this dynamically. But cant' find a starting point for this. If do you have any thought about this, it would be great if you can share. Thanks for reply.Valuate
if you use table view , also you can use rows dynamicallyGavingavini
Ok. Let me try. ThanksValuate
Did you get the solution to this issue ?Lodestar
S
0

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.

Suannesuarez answered 31/1, 2015 at 9:31 Comment(1)
if you are using two grid views , then I guess , table view would come more handyGavingavini
L
0

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

Letterperfect answered 21/7, 2016 at 15:44 Comment(1)
this should be downvoted. this will still not fix it if you 1 missing on your row, it wont be at center but will be replaced with invisible block.Cherellecheremis
C
0

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.

Cherellecheremis answered 23/4, 2019 at 23:30 Comment(0)
P
-1

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>
Pennoncel answered 19/1, 2014 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.