How to transition between activities with left to right slide animation

In this android programming source code example, we are going to transition between activities with left to right slide animation.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to transition between activities with left to right slide animation.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" 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"
    tools:context=".animation.Animation5">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slide Animation with Activity"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:paddingBottom="30dp"/>

    <Button
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slide Activity"
        android:onClick="slide"/>

</LinearLayout>

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


public class Animation5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation5);

    }

    public void slide(View view) {
        Intent intent = new Intent(this, Animation1.class);
        startActivityForResult(intent, 500);
        overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
    }
}

anim/slide_left.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:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:duration="3000"/>
</set>

anim/slide_right.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:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:duration="3000"/>
</set>

 

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment