Home » Android » android – Center elements in GridView

android – Center elements in GridView

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have GridView with 6 images in 2 columns. I want to display this 6 images in the middle of the screen. I set gravity properties to center but this center elements only horizontally. GridView takes whole screen.

<GridView
    android:id="@+id/gridview"
    android:layout_above="@id/ad" android:layout_alignParentTop="true"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="2"    
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"       
    android:background="#ffffff"
    android:gravity="center"
/>
How to&Answers:

Here is what I did to get the items in the gridview to center (notice the stretchmode, columnwidth, gravity and horizontal and vertical spacing):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp" >
    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dp" />
</LinearLayout>

It took me a while to figure it out, but messing with those different values I was able to get them to center.

Answer:

Try wrapping your GridView in a LinearLayout that has it’s child elements centered and change your gridview’s layout_height to “wrap_content”. ex:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:layout_gravity="center">
    <GridView 
      android:id="@+id/homeGridView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:numColumns="3"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:stretchMode="columnWidth"
      android:gravity="center" />
</LinearLayout>

Answer:

Need to set the following attributes:

android:stretchMode="columnWidth"

…and…

android:gravity="center"

Answer:

well, in case you are using a custom adapter thing to inflate single rowview inside your gridview then try to add android:gravity=”center” to the layout of your single_row.. this will do the trick hopefully 🙂
i.e,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ivClrBlock"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        />

    <TextView
        android:id="@+id/tvClrName"
        android:layout_width="88dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="@string/color_name"
        android:textSize="15sp" />

</LinearLayout>

Answer:

Issue occurred when I updated the gridview adapter from:

        gridView = inflater.inflate(R.layout.mobile, null);

to

        gridView = inflater.inflate(R.layout.mobile, parent, false);

so I just reversed back using a try catch if the old (null) version failed.

Answer:

Try to change this:
android:layout_height="fill_parent"
into
android:layout_height="wrap_content"

You are now telling your grid view to fill it’s parent height and width fully.

Answer:

Here is my answer:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="100dip"
        android:gravity="center"
        android:numColumns="3"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dip" />
</LinearLayout>

Don’t use padding or horizontalSpacing if you don’t need it as it may cause problems with other views.

Note also that fill_parent is already deprecated.

Answer:

I hope this will solve your problem:

  <LinearLayout
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <GridLayout
        android:id="@+id/gridlayou4x3"
        android:rowCount="4"
        android:columnCount="3"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="80"
        android:minWidth="25px"
        android:minHeight="25px" />
  </LinearLayout>

Answer:

I found that you just have the gridlayout wrap content on height and width.

android:layout_centerHorizontal="true"

It will center itself for the size it needs but if you have it matching the parent. You are technically centering it based with a lot of parent empty space offsetting your children.