Skip to content

Instantly share code, notes, and snippets.

@mzelzoghbi
Last active March 13, 2017 08:57
Show Gist options
  • Save mzelzoghbi/831eacf2b9020b144f909156f42d7917 to your computer and use it in GitHub Desktop.
Save mzelzoghbi/831eacf2b9020b144f909156f42d7917 to your computer and use it in GitHub Desktop.
Square Relative Layout view
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context) {
super(context);
}
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int size;
if (widthMode == MeasureSpec.EXACTLY && widthSize > 0) {
size = widthSize;
} else if (heightMode == MeasureSpec.EXACTLY && heightSize > 0) {
size = heightSize;
} else {
size = widthSize < heightSize ? widthSize : heightSize;
}
int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(finalMeasureSpec, finalMeasureSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment