2

i need to understand animation on Android.

For example, my application starts with an activity with a button in the bottom, when the user click on the button i want that another activity appears with an animation from bottom to top and i want that the button becomes the "header" of this second activity.

How can i achieve this?

Thank You

Daniele

Thank you to DecodeGnome for the answer! It works!

But i have some problem with the animation when i want to close this activity, i create a anim_out.xml:

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

but this doesn't work (the second parameter of overridePendingTransition what is used for?).

I try to call a new overridePendingTransition in onStop() function:

public void onStop(){
   super.onStop();
   overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);

}

But when i call finish to the second activity, i still see the default animation (from left to right)!

Thank you again to who'll help me.

1
  • I normally override the backbutton, @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); overridePendingTransition(R.anim.in, R.anim.out); return true; } return super.onKeyDown(keyCode, event); } Nov 8, 2011 at 10:19

2 Answers 2

4

1) Create a folder called anim in res folder

2) Add 2 new XML animations there (example, anim_in.xml & anim_out.xml)

3) put this line of code in the new activities onCreate:

overridePendingTransition(R.anim.anim_in, R.anim.anim_out);

Anim_in.xml example:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromXDelta="0%p"
        android:fromYDelta="100%p"
        android:toXDelta="0"
        android:toYDelta="0%p"
        android:duration="300" />
</set> 

4) Place the button (header) in the top of the layout of the second activity.

2
  • You can of course also override the animation when going back, just call overridePendingTransition() when creating the Intent fr going back. Oct 28, 2011 at 9:26
  • 1
    I was having the same problem, yet other questions on SO had overridePendingAnimation() on the line right after startActivity(intent). No. 3 is the key that others seem to forget or leave out.
    – Mike D
    Nov 4, 2011 at 16:51
-1

Use this code:

     public void onBackPressed() {
        super.onBackPressed();   
        overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.