30

I am facing a problem .

I have three activities and i need to start new activity with slide left.

Activity1

Activity2

Activity3

means enter image description here

when i click to button new activity should not display directly like what android behavior .

i want new activity come from right side and display on current screen.

anyone can provide me guidance. This is animation or anything else.

Monty
  • 3,205
  • 8
  • 36
  • 61
  • Start activity and then just apply transition effect. Check this http://www.mindfiresolutions.com/Animation-on-Switching-between-activities-1392.php And Also this : http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483 – Manish Dubey May 20 '13 at 11:19
  • thanks manish please put this in proper ans so i can mark as accepted – Monty May 20 '13 at 11:35
  • http://stackoverflow.com/a/6056067/1168654 – Dhaval Parmar May 20 '13 at 11:39
  • @CobraAjgar : I have added my comment to answer. By the way, there is only 2 simple link for accessing source code and more info. And only simple link is not allowed in answer section thats why I send it in comment. – Manish Dubey May 20 '13 at 11:45

5 Answers5

106

I'll try to help you with the following example:

res/anim/trans_left_in.xml

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

res/anim/trans_left_out.xml

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

res/anim/trans_right_in.xml

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

    <translate 
        android:fromXDelta="-100%p" 
        android:toXDelta="0" 
        android:duration="@android:integer/config_longAnimTime"/>
</set>

res/anim/trans_right_out.xml

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

src/Activity2

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traces);
        overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
...}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.trans_right_in, R.anim.trans_right_out);
}
Med Besbes
  • 2,001
  • 6
  • 25
  • 38
19

Start activity and then just apply transition effect. For more info, how to proceed for this just visit here and for source code example visit this. For any query, feel free to comment.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
  • one thing manish i need to start other activity also with the same time ..like click on A activity i need to show B,C sequentially.what should i do – Monty May 20 '13 at 12:13
  • You can't do without any intent on B Activity. One thing you can that just startActivity (C activity) in onCreate() of Activity B. – Manish Dubey May 20 '13 at 12:16
  • its directly displaying act C. – Monty May 20 '13 at 12:33
  • Then use startActivity(C Activity) in onStart of B activity and before starting use Thread.sleep or something else who will wait for execute to next line. – Manish Dubey May 20 '13 at 12:38
  • 1
    I can't help anymore without source code that you written. So, Ask a different question for different problem. Show there what have you tried and what are you expecting ? – Manish Dubey May 20 '13 at 12:48
  • You should try to give Self-sustained answers and not just links, because if they break your answer will be useless. – El Mac Oct 15 '16 at 11:18
11

A Better approach is to create a style as follows:

<style name="mytheme" parent="@android:style/Theme.Black">
    <item name="android:windowAnimationStyle">@style/theme</item>
</style>

<style name="theme">
    <item name="android:windowEnterAnimation">@anim/fade_in</item>
    <item name="android:windowExitAnimation">@anim/fade_out</item>
</style>

Then apply this style to your activity in manifest file using the android:theme tag.

nikhil.thakkar
  • 1,093
  • 7
  • 12
  • This was helpful from a learning point of view. But practically, I needed to reverse the animation when going backwards, which this didn't do. – chris838 Jun 28 '17 at 17:32
6

Here it is,

Intent intent=new Intent(Activity1.this,Activity2.class);
startActivityForResult(intent,0);
getActivity().overridePendingTransition( R.anim.righttoleft, R.anim.stable );

And here is the animation righttoleft.xml,

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >

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

and stable.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha android:fromAlpha="1.0" android:toAlpha="1.0" 
      android:interpolator="@android:anim/accelerate_interpolator" 
      android:duration="500"
      android:repeatCount="0"/>
</set>
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
3

call overridePendingTransition before starting the SecondActivity. It takes as parameters two int enterAnim, exitAnim.

  • enterAnim is a resource ID of the animation resource to use for the incoming activity. Use 0 for no animation
  • exitAnim is a resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
Blackbelt
  • 156,034
  • 29
  • 297
  • 305