Busata
Busata

Reputation: 1039

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

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.

Upvotes: 11

Views: 13464

Answers (5)

Zohab Ali
Zohab Ali

Reputation: 9574

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

Upvotes: 4

Konstantin Konopko
Konstantin Konopko

Reputation: 5418

This works for me

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

Upvotes: -3

JPLauber
JPLauber

Reputation: 1370

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.

Upvotes: 7

Busata
Busata

Reputation: 1039

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

Upvotes: 27

Bruno Carrier
Bruno Carrier

Reputation: 532

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

Upvotes: 2

Related Questions