1

I have two activities : FirstActivity and SecondActivity

I want the animation on click of buttons in this activities:

I am using following files:

FirstActivity.java

  package com.example.vanimation;

  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;

  public class FirstActivity extends Activity
  {
@Override
protected void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_first );
    ((Button)findViewById( R.id.NextButton )).setOnClickListener( new OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            //startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivityForResult(intent, 0);
            overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
        }
    });
}
}

SecondActivity.java

  package com.example.vanimation;

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;

  public class SecondActivity extends Activity
   {
@Override
protected void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_second );
    ((Button)findViewById( R.id.BackButton )).setOnClickListener( new OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            goBack();
        }
    });
}

private void goBack()
{
    finish();
    overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
}

@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
    if( keyCode == KeyEvent.KEYCODE_BACK )
    {
        goBack();
        return true;
    }
    return super.onKeyUp( keyCode, event );
}
}

activity_first.xml

<?xml version="1.0"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:background="#ccc">
<TextView android:text="Page 1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button android:layout_height="wrap_content" android:text="Next"
    android:layout_width="wrap_content" android:id="@+id/NextButton" />
</LinearLayout>

activity_second.xml

<?xml version="1.0"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:background="#aaa">
<TextView android:text="Page 2" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button android:layout_height="wrap_content" android:text="Back"
    android:layout_width="wrap_content" android:id="@+id/BackButton" />

anim/slide_in_left.xml

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

anim/slide_out_left.xml

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

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vanimation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.vanimation.FirstActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.vanimation.SecondActivity"
        android:label="@string/title_activity_second" >
    </activity>
</application>

But still animation is not working. On click of button activities changes without animation.

Any help to get the animation for it will be helpful for me.

dipendra
  • 235
  • 4
  • 12

3 Answers3

0

Use below code for animation effect

public class CrossfadeActivity extends Activity implements AnimationListener {

TextView txtMessage1, txtMessage2;
Button btnStart;

// Animation
Animation animFadeIn, animFadeOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crossfade);

    txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
    txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
    btnStart = (Button) findViewById(R.id.btnStart);

    // load animations
    animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.fade_in);
    animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.fade_out);

    // set animation listeners
    animFadeIn.setAnimationListener(this);
    animFadeOut.setAnimationListener(this);

    // button click event
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // make fade in element visible
            txtMessage2.setVisibility(View.VISIBLE);
            // start fade in animation
            txtMessage2.startAnimation(animFadeIn);

            // start fade out animation
            txtMessage1.startAnimation(animFadeOut);
        }
    });

}

@Override
public void onAnimationEnd(Animation animation) {
    // Take any action after completing the animation

    // if animation is fade out hide them after completing animation
    if (animation == animFadeOut) {

        // hide faded out element
        txtMessage1.setVisibility(View.GONE);
    }

    if(animation == animFadeIn){
        // do something after fade in completed

        // set visibility of fade in element
        txtMessage2.setVisibility(View.VISIBLE);
    }

}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

}
}

Instead of textview or anything you can use next activity code inside it. and instead of fadein.xml and fadeout.xml you can use your slide_in_left and other animation xml file.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • I have used following code for activity to activity animation: Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivityForResult(intent, 0); overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left ); But it do not worked. – dipendra Dec 03 '13 at 13:51
0

Check this if you are looking for Top-Bottom and Bottom-Up animations :

Android Animation Example

You can change the value of fromDelta and toDelta if you want from left-right and right-left animations.

Hope this helps.

Community
  • 1
  • 1
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0
If you want your animation working  in back press then overrride the onBackPress Method

@Override
public void onBackPressed() {
//  super.onBackPressed();

    Intent desiredIntent = new Intent(this
            , DesiredActivity.class);
    desiredIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    startActivity(desiredIntent);
    overridePendingTransition(0,0);

}


Use the below line after startActivity if you want no animation

    overridePendingTransition(0,0);

This will override the default  animation and do no animation. You can also give some custom animation if you like



    overridePendingTransition(R.anim.animation1,R.anim.animation2);
TanvirChowdhury
  • 2,498
  • 23
  • 28