Android Activity Animation Tutorial

In this tutorial you can learn how to animate two activities like android navigation drawer style.


Android two activity animation like navigation drawer.
Android two activity animation like navigation drawer.

  • First of all create 2 activities.
  1. MainActivity
  2. SubActivity
  • Then 2 XML Files (simple way two xml enough,but we need another two xml to accomplish reverse animation)
  1. slide_in_left.xml
  2. slide_out_left.xml


To crate that xml fallow this step
  • Right lick your project root folder and goto New-> then  Click Android XML File
  • Then Select Resource type as Tween Animation and select Root Element as translate,then give file name (slide_in_left.xml ). repeat this task to second xml ( slide_in_left.xml ).
  • Then add flowing codes to that files.

slide_in_left.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:duration="500"
  4.    android:fromXDelta="100%p"
  5.    android:toXDelta="0" />


 slide_out.left.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:duration="500"
  4.    android:fromXDelta="0"
  5.    android:toXDelta="-50%p" />

Now in MainActivity.java create a button and add this code in onClickListener under onClick event to start out second activity with animation.

  1. import android.os.Bundle;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.view.View;
  5. import android.widget.Button;
  6. public class MainActivity extends Activity {
  7.   @Override
  8.    protected void onCreate(Bundle savedInstanceState) {
  9.       super.onCreate(savedInstanceState);
  10.       setContentView(R.layout.activity_main);
  11.       final Button defaultButton = (Button) findViewById(R.id.button1);
  12.       defaultButton.setOnClickListener(new View.OnClickListener() {
  13.    
  14. @Override
  15.    public void onClick(View v) {                          
  16.    Intent subActivity = new Intent(MainActivity.this,SubActivity.class);                           
  17.         startActivity(subActivity);
  18.         overridePendingTransition(R.anim.side_in_left, R.anim.slide_out_left);//animation part
  19.                         }
  20.                 });
  21.         }
  22. }

This is the basic animation task.you can create another animation xml file with reverse values and call it from SubActivity.java by overriding onFinish() method.

  1. import android.os.Bundle;
  2. import android.app.Activity;
  3. public class SubActivity extends Activity {
  4.         @Override
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.activity_sub);
  8.         }
  9.         @Override
  10.         public void finish() {         
  11.                 super.finish();                overridePendingTransition( R.anim.slide_in_right,R.anim.slide_out_right);/*override and add your own[think 10 seconds and create that files :)] reverse animation xml files */
  12.         }
  13. }

Thats all have fun with android animation!

Comments

Post a Comment

Popular posts from this blog

Brother printer password reset using telnet

How to adjust the brightness in Samsung 19" SyncMaster SA100 LED monitor?

ASP.NET Server Controls Tutorial