Search code examples
androidandroid-layoutandroid-gridviewpopulate

Full Populate GridView with Images without padding


I am trying to populate my entire GridView, without any spaces between photos, no padding...

I am almost there, but there is a small bug, because I can scroll up and there is no photos... It's like I scroll up and there is a full WHITE page... Can't understand it.

This is my onCreate() method at activity class:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this, gridview));

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

This is my ImageAdapter class, the getView() method and some important attributes:

private Context mContext;
private GridView grid;

public ImageAdapter(Context c, GridView gridview) {
    mContext = c;
    this.grid = gridview;
}

// references to our images
private Integer[] mThumbIds = {
    R.drawable.sample_2, R.drawable.sample_3,
    R.drawable.sample_4, R.drawable.sample_5,
    R.drawable.sample_6, R.drawable.sample_7,
    R.drawable.sample_0, R.drawable.sample_1,
    R.drawable.sample_2, R.drawable.sample_3,
    R.drawable.sample_4, R.drawable.sample_5,
    R.drawable.sample_6, R.drawable.sample_7,
    R.drawable.sample_0, R.drawable.sample_1,
    R.drawable.sample_2, R.drawable.sample_3,
    R.drawable.sample_4, R.drawable.sample_5,
    R.drawable.sample_6, R.drawable.sample_7
};

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(
                grid.getWidth()/2,  grid.getHeight()/11));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

This is the layout, less important...

<?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="110dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

Solution

  • The problem may be in this line:

    imageView.setLayoutParams (new GridView.LayoutParams (grid.getWidth () / 2, grid.getHeight () / 11));
    

    Try replacing by this line:

    imageView.setLayoutParams(new GridView.LayoutParams (50, 50));
    

    This help you?