0

The Views of a GridView overlap when run on one (real) device running at 1024x600 and mdpi

wrong

but are correct

right

on another (also real) device running at 1280x720 and xhdpi as well as on several emulators, including one for the device producing the error.

Where does one start to look for the source of the problem? The layout of the GridView is a harmless

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

and, in this brief example, the adapter is equally simple.

public View getView( int position, View convertView, ViewGroup parent ) {
    MyView myView;
    if( convertView == null ) {  // if it's not recycled, initialize some attributes
        myView = new MyView( context );
        myView.setLayoutParams( new GridView.LayoutParams(150, 150) );
        myView.setPadding( 8, 8, 8, 8 );
    } else {
        myView = (MyView) convertView;
    }
    return myView;
}
Calaf
  • 10,113
  • 15
  • 57
  • 120

1 Answers1

0

In your XML you gave android:columnWidth="90dp" and 10dp both vertical and horizontal spacing and then in your custom view implementation you gave 150 as width and height, and 8 as padding, basically they conflict, try to make both values convenient since the columnWidth you gave is way more less than width you gave in your custom view,

Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • I see. What a silly mistake. Yet this also points out that the code in sdk/samples/android-17/ApiDemos is modest. There src/com/example/android/apis/view/Grid1.java contains i.setLayoutParams(new GridView.LayoutParams(50, 50)); while res/layout/grid_1.xml has android:columnWidth="60dp". – Calaf Aug 12 '13 at 14:59
  • You are right about the conflict between columnWidth in the XML file and the layoutParameters in the Java file, but I have the impression that the padding applies to the View within whatever the layout manager gave it, whereas the vertical and horizontal spacing in the XML file is a minimum suggestion. The layout manager may assign (much) more than this value. – Calaf Aug 12 '13 at 19:07
  • yes padding is within view whereas vertical/horizontal spacing is much like margin but even you involve that values, it still has conflicts, glad you solve your issue :) – Onur A. Aug 12 '13 at 22:37