How get height of the a view with gone visibility and height defined as wrap_content in xml?

The only way that i found was setting the width of the my view from width of a visible header view, then the below code returned me the right value.

int widthSpec = MeasureSpec.makeMeasureSpec(headerView.getWidth(), MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
v.measure(widthSpec, heightSpec);
int height = v.getMeasuredHeight();

view.post(new Runnable() {
    @Override
    public void run() {
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = view.getMeasuredHeight();
    }
});

This worked for me. The method "post" makes sure the view is already added to the screen.


When an object is gone, it no longer is part of the layout. Maybe you mean to set your object to invisible, in which case you'll probably get a sensical value

Tags:

Java

Xml

Android