Learn How to Slide Activity in Android

Introduction

 
This article explains the Sliding Activity in Android using Android Studio.
 
Before getting to the code we will first learn about the implementation.
 
In this first, you will create two XML files inside the anim folder that defines the animation to be performed on the button click.
 
anim1
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.            android:fromXDelta="100%p"  
  4.            android:toXDelta="0"  
  5.            android:duration="500"/> 
anim2
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.            android:fromXDelta="0"  
  4.            android:toXDelta="-50%p"  
  5.            android:duration="500"/> 
  • Activityoptions
    a helper class to build an option bundle to be used with "context.startActivity(Intent,Bundle)" and related methods.
  • makeCustomAnimation
    Create an "ActivityOptions" specifying a custom animation to run when the Activity is displayed. It returns an Activity Options type object that we to the "startActivity()" to perform the sliding operation.
Step 1
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.  >  
  11. <Button  
  12.        android:id="@+id/button"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_width="300dp"  
  15.         android:text="Slide"  
  16.         android:textStyle="italic"  
  17.    
  18.         android:textSize="40dp"  
  19.         android:layout_centerHorizontal="true"  
  20.         ></Button>  
  21. </RelativeLayout>   
Step 2
 
Create another XML file and write this: 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="vertical"  
  4.               android:layout_width="match_parent"  
  5.               android:layout_height="match_parent"  
  6.         android:background="#234567">  
  7.    
  8.    <ImageView  
  9.            android:layout_width="wrap_content"  
  10.            android:layout_height="wrap_content"  
  11.            android:src="@drawable/download"  
  12.            android:layout_gravity="center"  
  13.            android:layout_marginTop="100dp"  
  14.            ></ImageView>  
  15.    
  16. </LinearLayout>  
Step 3
 
Create another XML file inside the res folder named "anim1":
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.            android:fromXDelta="100%p"  
  4.            android:toXDelta="0"  
  5.            android:duration="500"/> 
anim2
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.            android:fromXDelta="0"  
  4.            android:toXDelta="-50%p"  
  5.            android:duration="500"/>  
Step 4
 
Create a Java file and write this:
  1. package com.slidingmenu;     
  2. import android.os.Bundle;    
  3. import android.app.Activity;    
  4. import android.app.ActivityOptions;    
  5. import android.content.Intent;    
  6. import android.view.Menu;    
  7. import android.view.View;    
  8. import android.widget.Button;    
  9.      
  10. public class MainActivity extends Activity {    
  11.      
  12.     @Override    
  13.     public void onCreate(Bundle savedInstanceState) {    
  14.         super.onCreate(savedInstanceState);    
  15.         setContentView(R.layout.activity_main);    
  16.         Button button = (Button)findViewById(R.id.button);    
  17.         button.setOnClickListener(new View.OnClickListener() {    
  18.             @Override    
  19.             public void onClick(View v) {    
  20.                 Intent slide = new Intent(MainActivity.this, Second.class);    
  21.                 Bundle bnaniamtion =ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.anim1,R.anim.anim2).toBundle();    
  22.                 startActivity(slide, bnaniamtion);    
  23.             }    
  24.         });    
  25.     }    
  26. }    
Step 5
 
Create another Java file and write this:
  1. package com.slidingmenu;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4.    
  5. /** 
  6.  * Created by vivek on 8/5/13. 
  7.  */  
  8. public class Second extends Activity {  
  9.    
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.second);  
  13.    
  14.     }  
  15. }
Step 6
 
Perform changes in the Android Manifest XML file as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.slidingmenu"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.slidingmenu.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     <activity  
  26.             android:name=".Second">  
  27.    
  28.     </activity>  
  29.     </application>  
  30.    
  31. </manifest>  
Step 7
 
image.jpg
 
Step 8
 
When you click on the button:
 
Clipboard02.jpg