How do I center a GridView in its LinearLayout parent?

android, android-linearlayout, gridview, layout

Solution

I don't know if you resolved your problem (i hope yes, and also i'm very late on answering on this post!), but here is a clue:

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

    <GridView
        android:id="@+id/gridview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:stretchMode="spacingWidth" >

    </GridView>
</LinearLayout>

Unfortunately this work only if there is 1 content on the grid. Maybe someone can improve it!

Problem

GridView is not behaving like it is supposed to. This screenshot shows that the GridView (in landscape mode) is flushed left. I want it centered. This is the XML layout for the GridView. ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/templatelandscape" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <GridView android:id="@+id/commandsbarlandscape" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:padding="0dp" android:verticalSpacing="2dp" android:horizontalSpacing="2dp" android:numColumns="auto_fit" android:columnWidth="52dp" android:stretchMode="spacingWidth" android:gravity="fill_horizontal" /> </LinearLayout> ``` What am I doing wrong ?

Original source