Search code examples
androidanimationandroid-activitytransition

Easy way to set a simple slide animation on Android activity transitions?


I'm a beginner developer and I'm trying to make a new app.

My app has a pretty simple navigation structure. It has a main menu and you just go deeper in the tree into specific categories.

I'd like my activities to transition with a quick slide. So from the main menu, detail activites slide from the right. Then when the user wants to go back, the main menu will slide from the left.

Looking around, I can't seem to find any good easy options. My minimum API is 21, so I'm trying to use Lollipop animations.

Putting code like this:

window.enterTransition = Slide(Gravity.START)
window.exitTransition = Slide(Gravity.END)
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())

...creates this unsatisfactory slide effect. The slide is more akin to a wave, with views not sliding all at once quickly.

I'd just like to be able to override the default activity animation which seems to appear from the bottom up. Is there some simple way to do this?


Solution

  • Check this:

    public static void switchActivityAnimation(Activity activity)
        {
            activity.overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
        }
    

    And
    slide_in

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" >
        <translate android:duration="1500" android:fromXDelta="100%" android:toXDelta="0%" />
    </set>  
    

    slide_out

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" >
        <translate android:duration="1500" android:fromXDelta="0%" android:toXDelta="-100%"/>
    </set>  
    

    I am not sure if this take required effect. I use it for fade in and fade out animation.

    Use it after startActivity()