How to set the equal column width for gridview in android?

Manikandan Palanisamy picture Manikandan Palanisamy · Aug 16, 2012 · Viewed 7.4k times · Source

In my application I have used the gridview and the textview is an item for the gridview. The number of columns = 3, In this how to give the equal width for all columns. Could anyone help me?

My code is:

Gridview: .........

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center" >

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/MyGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"
        android:gravity="center_horizontal"
        android:horizontalSpacing="5dp"
        android:numColumns="3"
        android:padding="5dp"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" >
    </GridView>
</LinearLayout>

Grid Item:

<TextView
    android:id="@+id/grid_item_text"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_weight="1"
    android:background="@drawable/btn_selected"
    android:gravity="center_horizontal|center_vertical"
    android:text="TextView"
    android:textColor="@color/BLACK"
    android:textSize="14dp" >
    </TextView>

</LinearLayout>

Answer

SilentKiller picture SilentKiller · Aug 17, 2012
<TextView
android:id="@+id/grid_item_text"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="@drawable/btn_selected"
android:gravity="center_horizontal|center_vertical"
android:text="TextView"
android:textColor="@color/BLACK"
android:textSize="14dp" >