29

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());
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
DastakWall
  • 377
  • 1
  • 7
  • 25

5 Answers5

50

You can do this using XML animation.

Create a slide-up animation XML using set and alpha and put this XML into your resource anim folder.

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

USE:

Use AnimationUtils.loadAnimation() to load animation from XML and set and start animation using .startAnimation() method.

Here is an example:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

// slide-up animation
Animation slideUp = AnimationUtils.loadAnimation(this, R.anim.slide_up);

if (imageView.getVisibility() == View.INVISIBLE) {
    imageView.setVisibility(View.VISIBLE);
    imageView.startAnimation(slideUp);
}

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • I have updated my answer. Its now showing slide up animation as like snackbar – Ferdous Ahamed May 23 '17 at 22:35
  • thanks for the reply, your solution is working too, and I'm gonna accept it because it looks cleaner to me. – DastakWall May 23 '17 at 23:00
  • Glad to know that. If my answer seems useful, please give an upvote. Thanks in advance – Ferdous Ahamed May 24 '17 at 02:59
  • thanks for the solution, I wanted to do the exact thing when I am trying to make the view GONE, but it doesn't work, and invisible the view at once, is there something I should do differently in this position? – Arash Afsharpour Dec 12 '20 at 12:48
  • hey @ArashAfsharpour same problem here, did you find any solution? – Vivek Thummar Dec 15 '20 at 10:17
  • @VivekThummar did the fix in other ways, without visible and gone, I used this: yourSecondView.animate().translationX(context.toPx(-280)); yourFirstView.animate().translationY(context.toPx(-98)); – Arash Afsharpour Dec 22 '20 at 09:51
18

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

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
  • This option is interesting, it even updates recycleView faster, even though i have implemented it on another layout. – Amir Dora. Apr 16 '20 at 12:39
13

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.

James
  • 4,573
  • 29
  • 32
ParSa
  • 1,118
  • 1
  • 13
  • 17
7

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>
Mayur Patel
  • 161
  • 1
  • 6
1

Based on this answer:

with this methods, I can set the visibility of my view to VISIBLE with a slideUp animation(Like snackbar animation):

int getScreenHeight() {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    return displaymetrics.heightPixels;
}

public void animateOnScreen(View view) {
    final int screenHeight = getScreenHeight();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", screenHeight, (screenHeight * 0.8F));
    animator.setInterpolator(new DecelerateInterpolator());
    animator.start();
}

Then I can use it like this:

if (myView.getVisibility() == View.INVISIBLE) {
    myView.setVisibility(View.VISIBLE);
    animateOnScreen(myView);
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
DastakWall
  • 377
  • 1
  • 7
  • 25