get horizontal and vertical spacing in gridview

dd619 picture dd619 · May 2, 2013 · Viewed 15.1k times · Source

I am implementing a grid view in my application and I want to get the horizontal spacing between consecutive rows and vertical spacing between consecutive columns within grid view using java code.

Is it possible? Is there any direct methods to get spacing?

Answer

Celta picture Celta · May 2, 2013

You can use android:verticalSpacing and android:horizontalSpacing in GridView tag and provide the spacing as per your requirement.

For example:

<GridView
    android:layout_height="wrap_content"
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:numColumns="auto_fit"
    android:horizontalSpacing="10dp"       // space between two items (horizontal)
    android:verticalSpacing="10dp">        // space between two rows (vertical)

On java try:

"gridviewname".getHorizontalSpacing()
"gridviewname".getVerticalSpacing()