Busata Busata - 1 year ago 230
Android Question

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

I have a ListView with visibility=gone and height=wrap_content, and i need its height to can make an animation of expand.

I already tried it:

view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int height = view.getMeasuredHeight();


v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int height = view.getMeasuredHeight();


v.measure(View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED));
int height = view.getMeasuredHeight();

But it returned me a lesser value.

I am trying to do this after onResume.

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download