Android: get height of a view before it´s drawn

For an animation I need to know the height from a View. The Problem is, that the getHeight() method allways return 0 unless the View is drawn.
So is there any way to get height without drawing it?

In this case the View is a LinearLayout.

I try to adapt the expand Animation from

With it I want to expand some more informations for a list item.
I wasn´t able to achieve the same effect via xml.
At the moment the Animation only works when you know the layout size before drawing it.


Sounds like you want to get the height but hide the view before it is visible.

Have the visibility in the view set to visible or invisible to start with(just so that a height is created). Don’t worry we will change it to invisible/gone in the code as shown below:

private int mHeight = 0;
private View mView;


// onCreate or onResume or onStart ...
mView = findViewByID(;
    new OnGlobalLayoutListener(){

        public void onGlobalLayout() {
            // gets called after layout has been done but before display
            // so we can get the height then hide the view

            mHeight = mView.getHeight();  // Ahaha!  Gotcha

            mView.getViewTreeObserver().removeGlobalOnLayoutListener( this );
            mView.setVisibility( View.GONE );


Source : Link , Question Author : anonymous , Answer Author : Jim Baca

Leave a Comment