First, you want to create your own View class, which you can use instead of the default LinearLayout. Then you want to override the call to View onMeasure and make it square:
public class GridViewItem extends ImageView { public GridViewItem(Context context) { super(context); } public GridViewItem(Context context, AttributeSet attrs) { super(context, attrs); } public GridViewItem(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, widthMeasureSpec);
Then you can change the row_grid.xml file to:
<path.to.item.GridViewItem xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" > </path.to.item.GridViewItem>
Just remember to change "path.to.item" to the package that contains the GridViewItem.java class.
Edit:
Also changed the scaleType from fitXY to centerCrop so that your image does not stretch and maintain its aspect ratio. And, as long as this is a square image, nothing should be cropped, independently.
source share