Skip to content

Instantly share code, notes, and snippets.

@kitek
Created May 29, 2013 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitek/5670362 to your computer and use it in GitHub Desktop.
Save kitek/5670362 to your computer and use it in GitHub Desktop.
Example of square gridview items
<?xml version="1.0" encoding="utf-8"?>
<com.your.app.SquareView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:scaleType="centerCrop" />
</com.your.app.SquareView>
package pl.garnek;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class SquareView extends ViewGroup {
public SquareView(Context context) {
super(context);
}
public SquareView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public SquareView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int u, int r, int d) {
getChildAt(0).layout(0, 0, r - l, d - u); // Layout with max size
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(0);
child.measure(widthMeasureSpec, widthMeasureSpec);
int width = resolveSize(child.getMeasuredWidth(), widthMeasureSpec);
child.measure(width, width); // 2nd pass with the correct size
setMeasuredDimension(width, width);
}
}
@iganovir
Copy link

i'm stilll confused , could you please make a full example? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment