New Activity in Android "enter from the side"
Asked Answered
D

5

37

What's the best way to go to a new activity but make it look like the activity slides to the left and the new activity enters the screen? I am using intents to call the new activity, is that the way to do it if I want the application to be lightweight?

To explain a bit better: on my Android phone I can swipe the view with the home meny to the right and then enters a friend stream from the left and takes place on the screen. I want to do it in my app with buttonclicks, it's the "sliding" i am after. THanks!

Daleth answered 26/1, 2011 at 16:34 Comment(0)
P
91

In android OS 2.1 or later I think you can use the OverridePendingTransition() method to provide the kind of transition between activities animations you are looking for.

Firstly, define a couple of animation resources in /res/anim/. Here is one that is named right_slide_out.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="500"
    />
</set>

An another called right_slide_in.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="700"
    />
</set>

Then, when you are starting the new activity, use the OverridePendingTransition method as in:

startActivity(intent);
overridePendingTransition  (R.anim.right_slide_in, R.anim.right_slide_out);

That should handle the transition animations for starting the activity.

For the reverse, when that activity finishes and youre coming back to the original one, it's a bit more foggy.

If you have some UI control that ends that activity and calls Activity.finish(), then you can just add the overridePendingTransition() right after that.

To handle the case where the user ends the activity by pressing the back button, use something like the following:

@Override
public void onBackPressed() 
{

    this.finish();
    overridePendingTransition  (R.anim.right_slide_in, R.anim.right_slide_out);
}
Protrusion answered 26/1, 2011 at 17:41 Comment(5)
Thanks a lot! Together with the API demo this really did it. It is easy to adjust as well. I want my app to go "through" my activities like the pages of a book, so this is a start. Very nicely explained.Daleth
I get it to work, the first activity sliding in from the right, and the other activity sliding in from the left. It seems they are now always at top Z-position when "coming into the screen". I want the first activity to move away (out of the screen) as the other one approaches. It seems I can't reach it as the other is coming in? Can I do a double overridePendingTransition? Thanks!Daleth
I doubt you cant use overridePendingTransition twice to good effect. If youre looking for a page turning effect I doubt it can be done with Activity transitions as they always run both animations simultaneously. Depending on your app you might be able to consider not switching activities at all, and instead keeping one activity and using multiple views so you can animate one view out and the other in, keeping them aligned so they appear the way you want. Animating views is much more flexible than what you can control with activity transitions.Protrusion
I now get that I can't use overridePendingTransition because one view cannot hold two activities at the same time. But in one forum I read that, like you guys said, the slide is default, but I have to turn it on in the emulator "The default transition between activity is this animation.. you can enable it on the emulator in devtools->developement settings". I can't find these settings, where are they? And my phone doesn't either show the slide, is it something I have to turn on?Daleth
Instead of overriding the onBackPress() you can also override onPause() since that is always called. I generally try to avoid overriding the onBackPress(). Also if you are overriding onBackPress() do super.onBackPressed() instead of finish() incase you have Base classes... Yes, pedantic but good semantics.Higbee
C
4

You could use a left_slide_out.xml (just change the toXDelta in Josh's right_slide_out.xml to read -100%p), to make the old activity disappear to the left (and also have the same duration on both of the animations).

Chair answered 13/7, 2012 at 10:35 Comment(0)
W
3

Yes, using intents is the standard way to start another activity, and usually does the sliding thing you mention. e.g. startActivity( new Intent( this, myNextActivity.class ) ); will do it.

Wychelm answered 26/1, 2011 at 16:42 Comment(1)
actually not... So when I start a new activity, it (kinda) bumps into my view with an ugly effect...Eldwon
T
3

Jems is correct. By default, you will get a sliding animation when launching a new intent.

If you are looking for a more custom animation, you can use overridePendingTransition. Keep in mind it was added in API Level 5. See this API demo for sample usage.

Tribesman answered 26/1, 2011 at 17:25 Comment(3)
Thanks for the API demo-link, it's a djungle sometimes in there. But just so I understand: what appears to me to be the default animation when launching a new intent, is a sort of disappearance of the existing activity, and that the one I call expands from the center, like from "inside the screen". I tried both on an AVD and my device. Am I missing something?Daleth
kakka47, I believe the default transitions changed as of Ice Cream Sandwich, but it might have been with Honeycomb--not sure.Whitman
Where is the API demo now?Marleah
E
1

There is a bit of a confusion as to what the enter and exit animations are. For those who are still pondering over it, here is a template..

overridePendingTransition(
    enterAnimationForCalledActivity, 
    exitAnimationForCallingActivity
);

This should probably clear the air a bit.

Entozoic answered 9/3, 2018 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.