3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="#FFFFFF"
    android:layout_gravity="center"
    android:padding="5dip" >
    <GridView
        android:id="@+id/homeGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="200dip"
        android:adjustViewBounds="true" 
        android:layout_gravity="center"
        android:gravity="center"
        android:horizontalSpacing="0dip"
        android:numColumns="2"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="10dip" />
</LinearLayout>
  1. I have four icons in my grid view, but the spacing between them is too much. I want to reduce it.
  2. Also i want to make the GridView center to the device, but it always stays at top.
theJava
  • 14,620
  • 45
  • 131
  • 172

2 Answers2

6

You should use dimension to overcome this issue. Just define dimension for each density screen.

  • values-ldpi
  • values-mdpi
  • values-hdpi
  • values-xhdpi
  • values-large

values-ldpi/dimesion.xml

<resources>
  <dimen name="grid_vertical_space">15dp</dimen>
</resources>

values-mdpi/dimesion.xml

<resources>
  <dimen name="grid_vertical_space">20dp</dimen>
</resources>

values-hdpi/dimesion.xml

<resources>
  <dimen name="grid_vertical_space">30dp</dimen>
</resources>

Like wise..

Try like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="#FFFFFF"
    android:layout_gravity="center"
    android:padding="5dip" >
    <GridView
        android:id="@+id/homeGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="200dip"
        android:adjustViewBounds="true" 
        android:layout_gravity="center"
        android:gravity="center"
        android:horizontalSpacing="0dip"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="@dimen/grid_vertical_space" />
</LinearLayout>
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
  • Thanks Amit, the horizontal space is still the same shown in the image and the entire grid is not coming down... – theJava Mar 22 '13 at 10:37
0

If your layout is composed only of 4 icons, why don't you use a TableLayout with 2 TableRow? I think you can achieve what you want more easily, because:

  1. you can define the spacing between items;
  2. also you can center your table.
JJ86
  • 5,055
  • 2
  • 35
  • 64
  • http://stackoverflow.com/questions/15570372/making-the-gridview-center-in-android can you help me out in this thread... i have almost done but yet not able to do it... i don't want to use TableLayout and TableRow. – theJava Mar 22 '13 at 12:31
  • @theJava done, but if you have an answer for this thread, publish it ;) . – JJ86 Mar 22 '13 at 12:50