Last active
March 13, 2017 08:57
-
-
Save mzelzoghbi/831eacf2b9020b144f909156f42d7917 to your computer and use it in GitHub Desktop.
Square Relative Layout view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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