Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getHeight for View which has visibility=gone

I have a LinearLayout whose visibility is set to "Gone" by default, I need to get the height of this view to do a slide-out animation when it will be visible. How do i get the total heigh of the visible state, because the View.getHeight returns zero when the layout is not called.

<LinearLayout
    android:id="@+id/card_checkin_layout_termsconditionsconfirmation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:gravity="center_horizontal"
    android:background="#d0d0d0"
    android:visibility="invisible"
    android:orientation="vertical" >

    <Button
        android:id="@+id/card_checkin_button_confirmdetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/shape_checkin_buttons2"
        android:text=">   Confirm your details"
        android:paddingLeft="8dp"            
        android:gravity="left|center_vertical"
        android:textColor="@color/card_checkin_button_textcolor_blue" 
        />

    <Button
        android:id="@+id/card_checkin_button_termsandconditions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="8dp"            
        android:background="@drawable/shape_checkin_buttons2"
        android:text=">   Terms and Conditions"
        android:gravity="left|center_vertical"
        android:textColor="@color/card_checkin_button_textcolor_blue" 
        />

</LinearLayout>
like image 437
revolutionary Avatar asked Aug 16 '13 15:08

revolutionary


1 Answers

Initially set the visibility of the view to be visible or invisible to start with, so that the height is calculated. Later change the visibility to gone.

FYI : removeGlobalOnLayoutListener() is deprecated since API level 16, it's replaced by removeOnGlobalLayoutListener().

You can try this:

// onCreate or onResume or onStart ...
mView = findViewByID(R.id.someID);
mView.getViewTreeObserver().addOnGlobalLayoutListener( 
    new OnGlobalLayoutListener(){

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

            mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            mView.setVisibility(View.GONE);
        }

});
like image 188
Ritesh Gune Avatar answered Nov 14 '22 08:11

Ritesh Gune