I've create a custom view based on LinearLayout
and I need to calculate the layout width and height according to the parent view but each time I use ((View)this.getParent()).getMeasuredWidth()
I got 0
- I guess that I called this function too early!
Any ideas to how accomplish this functionality?
try this
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
viewWidth = view.getParent().getWidth();
viewHeight = view.getParent().getHeight();
}
});
}
in Kotlin you can use like this :
val height = (parent as View).height
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With