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();
and
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int height = view.getMeasuredHeight();
and
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.
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.
There is only one efficient way to do that if you want to animate height of a view whose visibility is gone
First add this in xml of hidden view
android:animateLayoutChanges="true"
then thats how you can set it to animate
yourView.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
Android will animate any layout change on that specific view
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
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