Search code examples
androidgridviewselectormargins

Android GridView imperfection, how to remove extra white space to the right


I have a GridView based calendar. I have the following XML layout with the selector set to null thus android:listSelector="@null" in accordance with advise I have got from this site. Now I am getting a few pixels wide strip to the right of the GridView. Why? I have tried everything I can but to avail. Here is my XML layout:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <GridView
                android:id="@+id/calendar"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:horizontalSpacing="-1px"
                android:numColumns="7"
                android:stretchMode="columnWidth" 
                android:gravity="center"
                android:verticalSpacing="-1px"
                android:listSelector="@null" >

            </GridView>

        </LinearLayout>

What I get is this picture:

enter image description here


Solution

  • This space is due to imperfect calculation for each row of your grid. For example your device width is 320 px and you have 7 rows, try any calculation that meets 320 px. If the width of each cell is 45.71428571428571 px, only then it can be reduced.

    Other option

    apply android:gravity="center" property in you grid so that spaces will equally divided from left to right