0

I am trying to get the view's height that would be after expending it. I am using below View.MeasureSpec to measure.

public static void expand(View summary) {
    //set Visible
    summary.setVisibility(View.VISIBLE);
    summary.measure(
            View.MeasureSpec.makeMeasureSpec(((View)summary.getParent()).getMeasuredWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    ValueAnimator mAnimator = slideAnimator(0, summary.getMeasuredHeight(), summary);
    mAnimator.start();
}  

Folling this Android getMeasuredHeight returns wrong values ! and http://developer.android.com/reference/android/view/View.MeasureSpec.html

If I get the correct height the layout will be like belowenter image description here

But right now I am getting like below
enter image description here

Please suggest me some solution.

Community
  • 1
  • 1
umesh
  • 1,148
  • 1
  • 12
  • 25

1 Answers1

0

Try using the ViewTreeObserver to notify you when the View has finished measurments and layouting; it should be able to tell you it's exact size then.

Sample code:

    ViewTreeObserver vto = view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            int bodyOffset = 0;
            if (textPart != null) {
                bodyOffset = textPart.getTop();
            }

            Log.d(Constants.LOGTAG, "body bottom = " + (body.getBottom() + bodyOffset));
            Log.d(Constants.LOGTAG, "ok button top = " + okButton.getTop());

            if (body.getBottom() + bodyOffset > okButton.getTop()) {
                body.setVisibility(View.GONE);
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });
Shark
  • 6,513
  • 3
  • 28
  • 50