I have a view view1
which extends FrameLayout and overrides onMeasure() like this, so that it is rendered as a square:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
Now I want to create another view view2
which extends view1
, but now I want view2
to have a height equal to the parent, and hence to cover the full screen. As of now, I get it as a square too.
How do I achieve this?
In view 1:
private boolean measureAsSquare = true;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (measureAsSquare) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setMeasureAsSquare(boolean b) {
measureAsSquare = b;
}
In View2 constructor that subclasses view 1:
public View2(Context c) {
setMeasureAsSquare(false);
}
Then you can use normal layout weight and height for view 2 to assign its preferred size.