How to force GridView to generate square cells in Android
First, you're going to want to create a custom View class that you can use instead of the default LinearLayout you're using. Then you want to override the View's onMeasure call, and force it to be 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); // This is the key that will make the height equivalent to its width
}
}
Then you can change your 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 be sure to change "path.to.item" to the package where your GridViewItem.java class resides.
I also removed your LinearLayout parent since it wasn't doing anything for you there.
Edit:
Also changed scaleType from fitXY to centerCrop so that your image doesn't stretch itself and maintains its aspect ratio. And, as long as it's a square image, nothing should be cropped, regardless.
Square cells in GridView
Make sure your cell is square.
For this, you can set the width and height of the RelativeLayout programmatically with the value screen_width/4.
Flutter Gird View - Square items
Remove the Padding
around the Card
and specify the spacing between the elements in your GridView
using:
mainAxisSpacing
crossAxisSpacing
GridView to make square board
Create your own class MyGridView
which extends GridView
.
Override its onMeasure(...)
method and set height == width, like so:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
Then, create your own GridViewItem
extending a View
subclass e.g. TextView
or ImageView
etc. Override its onMeasure(...)
method in the same way.
MyGridView
and its items will all appear square.
Related Topics
How to Use Getsystemservice in a Non-Activity Class (Locationmanager)
How to Detect Device Is Android Phone or Android Tablet
How to Resolve Target 'Android-Xx'
Firebase Overwrites Signin with Google Account
Android Facebook 4.0 Sdk How to Get Email, Date of Birth and Gender of User
Adt Requires 'Org.Eclipse.Wst.Sse.Core 0.0.0' But It Could Not Be Found
Android Volley - How to Isolate Requests in Another Class
Compile with Proguard Gives Simexception: "Local Variable Type Mismatch"
In Androidx.Fragment.App.Fragment,Setuservisiblehint()Is Deprecated,And Not Executed,Why
Android Fade in and Fade Out with Imageview
Device Not Detected in Eclipse When Connected with Usb Cable
Android Facebook Sdk 3.0 Gives "Remote_App_Id Does Not Match Stored Id" While Logging In
Adding an Onclicklistener to Listview (Android)
Simulate Low Network Connectivity for Android