Java – Android GridView spacing and centering issues

Android GridView spacing and centering issues… here is a solution to the problem.

Android GridView spacing and centering issues

<?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 GridView, but the spacing between them is
    Too much. I want to reduce it.
  2. I also want the GridView to be the hub of the device, but it always is
    Stay ahead of the curve.

Solution

You should use dimensions to solve this problem.
Simply define the dimensions for each density screen.

  • values-ldpi
  • Value – mdpi
  • HDPI value
  • Value – xhdpi
  • Large value

values-ldpi/dimesion.xml

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

Value – MDPI/Dimes Ion .xml

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

Value – hdpi/dimesion .xml

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

Like the wise men…

Try it this way

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

Related Problems and Solutions