Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Activity in Android "enter from the side"

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!

like image 365
kakka47 Avatar asked Jan 26 '11 16:01

kakka47


People also ask

How do you launch an activity that is part of some other application?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);

How do I go back to first activity on Android?

Declare A in your manifest with the android:launchMode="singleTask" . This way, when you call startActivity() from your other activies, and A is already running, it will just bring it to the front. Otherwise it'll launch a new instance.


2 Answers

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); } 
like image 128
mmeyer Avatar answered Oct 13 '22 18:10

mmeyer


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).

like image 35
Andreas Berheim Brudin Avatar answered Oct 13 '22 16:10

Andreas Berheim Brudin