Reputation: 555
I have included some images in the gridview, at the end of the scroll, seems to come out of the margins without any reason. (see the image )Can anyone tell me why? I post the XML file and the Image Adapter.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gridView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:numColumns="4"
android:layout_marginBottom="20dp"/>
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context c) {
context = c;
}
//---returns the number of images---
public int getCount() {
return imageName.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageName[position]);
return imageView;
}
}
Upvotes: 0
Views: 82
Reputation: 51
This should help.
Basically, change this line to avoid hardcoding the pixel sizes.
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
The top answer here will show you how to get the current screen size (height/width). Use those values instead of 185.
Upvotes: 1