Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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();

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.

like image 373
Busata Avatar asked Apr 13 '15 17:04

Busata


4 Answers

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();
like image 138
Busata Avatar answered Oct 19 '22 07:10

Busata


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.

like image 27
JPLauber Avatar answered Oct 19 '22 07:10

JPLauber


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

like image 20
Zohab Ali Avatar answered Oct 19 '22 07:10

Zohab Ali


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

like image 27
Bruno Carrier Avatar answered Oct 19 '22 08:10

Bruno Carrier