theJava
theJava

Reputation: 15034

Android GridView spacing and center issue

<?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.

Upvotes: 3

Views: 3041

Answers (2)

JJ86
JJ86

Reputation: 5113

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.

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 8939

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>

Upvotes: 6

Related Questions