JGuizard
2/12/2016 - 6:54 PM

Center an item horizontally (and maybe vertically) inside a GridView

Center an item horizontally (and maybe vertically) inside a GridView

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_weight="0.8">

            <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/gridAnswer"
                android:layout_width="fill_parent"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="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>
        
        
        <!--ITEM.XML -->
        
        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
        android:id="@+id/btLetter"
        android:textSize="24dp"
        android:gravity="center"
        android:padding="5dp"
        android:textIsSelectable="false"
        android:textColor="@android:color/black"
        android:background="@drawable/frame_letter_answer"/>
</LinearLayout>