Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MeasuredHeight always 0, and how can I get the value?

I'm trying to apply an animation to collapsing a LinearLayout. This animation depends on the MeasuredHeight property of the LinearLayout having a value greater than 0, but the value is always 0.

This is the code I'm using:

    public void Collapse()
    {
        var v = FindViewById<LinearLayout>(Resource.Id.layoutBranding);
        int initialHeight = v.MeasuredHeight; // always 0!!

        var a = new MyAnimation(v, initialHeight);

        a.Duration = (int)(initialHeight / v.Context.Resources.DisplayMetrics.Density);
        v.StartAnimation(a);
    }

Where MyAnimation is defined as:

public class MyAnimation : Animation { private readonly View _view; private readonly int _initalHeight;

    public MyAnimation(View view, int initalHeight)
    {
        _view = view;
        _initalHeight = initalHeight;
    }

    protected override void ApplyTransformation(float interpolatedTime, Transformation t)
    {
        if (interpolatedTime == 1)
        {
            _view.Visibility = ViewStates.Gone;
        }
        else
        {
            _view.LayoutParameters.Height = _initalHeight - (int) (_initalHeight*interpolatedTime);
            _view.RequestLayout();
        }
    }

    public override bool WillChangeBounds()
    {
        return true;
    }
}
like image 687
DaveDev Avatar asked Sep 19 '25 13:09

DaveDev


1 Answers

try this:

v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);


int initialHeight = v.MeasuredHeight; 
like image 124
dasdasd Avatar answered Sep 21 '25 05:09

dasdasd