1

I have an animation to view Slide up and down in an Activity, I'm using the below code. In the first time, I'm getting the rlView height as "0" once the animation ends only I get the proper height

private void animateView() {
        Log.d(TAG, "animateView: height" + markLocationBinding.rlView.getHeight());
        if (markLocationBinding.ivHurray.getVisibility() == View.GONE) {
            markLocationBinding.ivHurray.setVisibility(View.VISIBLE);
            Log.d(TAG, "animateView: inside height" + markLocationBinding.rlView.getHeight());
            markLocationBinding.ivHurray.animate()
                    .translationY(markLocationBinding.rlView.getHeight())
                    .alpha(1.0f)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            Log.d(TAG, "animateView: inside height end" + markLocationBinding.rlView.getHeight());
                        }
                    });
        } else {
            markLocationBinding.ivHurray.animate()
                    .translationY(0)
                    .alpha(0f)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            markLocationBinding.ivHurray.setVisibility(View.GONE);

                        }
                    });
        }
    }

My layout

 <RelativeLayout
            android:id="@+id/ivHurray"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="-450dp"
            android:background="#bb000000"
            android:clickable="true"
            android:orientation="vertical"
            android:visibility="gone">

            <RelativeLayout
                android:id="@+id/rlView"
                android:layout_width="match_parent"
                android:layout_height="450dp">

                <LinearLayout
                    android:id="@+id/llText"
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    android:layout_below="@+id/ivMan"
                    android:layout_marginTop="-30dp"
                    android:background="#94aea1"
                    android:gravity="bottom"
                    android:orientation="vertical"
                    android:paddingBottom="15dp">

                    <TextView
                        android:id="@+id/tvHurray"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="8dp"
                        android:text="@string/hurray"
                        android:textColor="@color/white"
                        android:textSize="22dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/tvHurraySub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="5dp"
                        android:text="@string/geo_marked_loc"
                        android:textColor="@color/white"
                        android:textSize="18dp" />
                </LinearLayout>
      <ImageView
                android:id="@+id/ivMan"
                android:layout_width="match_parent"
                android:layout_height="320dp"
                android:background="@drawable/hurray_image"
                android:scaleType="center" />

            </RelativeLayout>

//some other views           
        </RelativeLayout>
Aabauser
  • 533
  • 1
  • 4
  • 17

2 Answers2

3

You need to wait until your views have finished being laid out before you can get their height/width. You can do this using ViewTreeObserver

markLocationBinding.rlView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //Need to remove the layout listener otherwise animateView() will be called every time view updates
            rlView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            animateView();
        }
    });
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
1

You can get measured height and width like below:

view.measure(0,0);
int width=view.getMeasuredWidth(); 
int height=view.getMeasuredHeight();
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43