I've been trying to achieve a GridLayout
with square children as you can see from this image.
So basically the view should adjust depending on the number of items without me declaring the column size and row size. Is this possible for GridLayout
? If not then forget about this question.
Now lets get to the square part. I tried implementing this code
public class SquareLinearLayout extends LinearLayout {
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSize == 0 && heightSize == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(minSize, minSize);
return;
}
final int size;
if (widthSize == 0 || heightSize == 0) {
size = Math.max(widthSize, heightSize);
} else {
size = Math.min(widthSize, heightSize);
}
final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(newMeasureSpec, newMeasureSpec);
}
}
though it does make the child layout square it also occupies the entire width of the screen pushing the other children out of the viewable part of the layout.
Do you have any links or you yourself have a code that is similar to this class but does not occupy the entire width of the layout? Thanks in advance.