[embedyt] https://www.youtube.com/watch?v=0s6x3Sn4eYo[/embedyt]
To add the animations, right-click on anim -> New -> Animation resource file.
slide_in_left.xmlslide_in_right.xmlslide_out_left.xmlslide_out_right.xmlMainActivity.javaactivity_main.xmlActivity2.javaactivity_2.xmlActivity3.javaactivity_3.xmlstyles.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_mediumAnimTime" android:fromXDelta="-100%p" android:toXDelta="0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_mediumAnimTime" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_mediumAnimTime" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_mediumAnimTime" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
package com.codinginflow.activityslideanimationexample; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openActivity2(View view) { Intent intent = new Intent(this, Activity2.class); startActivity(intent); overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.activityslideanimationexample.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is Activity 1" android:textSize="30sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="openActivity2" android:text="Open Activity 2" /> </LinearLayout>
package com.codinginflow.activityslideanimationexample; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); } public void openActivity3(View view) { Intent intent = new Intent(this, Activity3.class); startActivity(intent); } @Override public void finish() { super.finish(); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.activityslideanimationexample.Activity2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is Activity 2" android:textSize="30sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="openActivity3" android:text="Open Activity 3" /> </LinearLayout>
package com.codinginflow.activityslideanimationexample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class Activity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_3); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.activityslideanimationexample.Activity3"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is Activity 3" android:textSize="30sp" /> </LinearLayout>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item> </style> <style name="CustomActivityAnimation" parent="@android:style/Animation.Activity"> <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item> <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item> <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item> <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item> </style> </resources>