5

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?

Pankaj Dubey
  • 796
  • 3
  • 8
  • 32
dd619
  • 5,910
  • 8
  • 35
  • 60

2 Answers2

2

You can also try in Java

mGridView.setHorizontalSpacing(4);
mGridView.setVerticalSpacing(4);

Or in XML

<GridView
    android:layout_height="wrap_content"
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:numColumns="auto_fit"
    android:horizontalSpacing="4dp"  
    android:verticalSpacing="4dp">
Matthew Usdin
  • 1,264
  • 1
  • 19
  • 20
1

If your minSdk is 16 then you can use mGridView.getHorizontalSpacing() or mGridView.getVerticalSpacing().

If not, you can set in the xml layout android:verticalSpacing="@dimen/grid_spacing" and in the code use getResources().getDimensionPixelSize(R.dimen.grid_spacing).

This way, you can change the size in just one place and it'll effect both the xml-layout and the code.

Inside /res/values/dimens.xml add this line -

<dimen name="grid_spacing">10dp</dimen>
Aviv Ben Shabat
  • 1,073
  • 2
  • 13
  • 33