Animate visibility of a view from gone to visible with animation

Viewed 45873

I have a view that is invisible by default(Just for the first time).

Now I need to switch the visibility to VISIBLE with this animation:

if (myView.getVisibility() == View.INVISIBLE) {
    myView.setVisibility(View.VISIBLE);
    myView.animate().translationY(0);
 }

(Like the SnackBar default animation)

But this isn't working. It will turn visible with default animation

Is there any simple way that I could achieve this?

Note

I'm animating my view to dismiss, like this:

myView.animate().translationY(myView.getHeight());
5 Answers

Add animations using ConstraintLayout

Just add below code above the views whose visibility is updated:

TransitionManager.beginDelayedTransition(constraintLayout)

Note:

  • ConstraintLayout will only perform animation on its direct children since it only knows when you change layout parameters and constraints on the children that it handles.
  • ConstraintLayout only animates layout related changes.

For more see this post https://robinhood.engineering/beautiful-animations-using-android-constraintlayout-eee5b72ecae3

This is the best way to animate views visibility :

private void viewGoneAnimator(final View view) {

    view.animate()
            .alpha(0f)
            .setDuration(500)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    view.setVisibility(View.GONE);
                }
            });

}

private void viewVisibleAnimator(final View view) {

    view.animate()
            .alpha(1f)
            .setDuration(500)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    view.setVisibility(View.VISIBLE);
                }
            });

}

And then call this method wherever you wanted to make a view visible or gone and give the intended view to methods as the parameter.

Just You need to add android:animateLayoutChanges="true" to your layout. When I set visibility gone to linear_container, linear_bottom will animate from bottom to up and take place of "linear_container".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:layout_height="match_parent">
       <android.support.design.widget.AppBarLayout
            android:id="@+id/layoutTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.design.widget.AppBarLayout>

       <LinearLayout
            android:id="@+id/linear_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
       </LinearLayout>
       <LinearLayout
            android:id="@+id/linear_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
       </LinearLayout>
</LinearLayout>
Related