1

I would like to switch the activities using animation.. I have a view with 5 images and with that i set my oncliclk listener. My main activity is

private View.OnClickListener onClickListener = new View.OnClickListener()
      {
        public void onClick(View testView)
        {
          if (testView == Main.this.myinput)
          {   
         Intent i = new Intent(Main.this, Activity1.class);
        startActivity(i);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
          }

       if (testView == Main.this.myinput)
          {   
         Intent i = new Intent(Main.this, Activity2.class);
        startActivity(i);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
          }
        }
      };

and my animation files are fade_in:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="0.0" 
           android:toAlpha="1.0"
           android:duration="500" />

fade_out:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromAlpha="1.0" android:toAlpha="0.0"
           android:fillAfter="true"
           android:duration="500" />

Now my problem is when switching between activities, animation has no effects on real devices but which is working on emulator.... I referred to stack overflow and googled but couldn't find why animation is not working.. I tried this with various type of animations such as slide_in_left,right,top,bottom... but animation is not working. Help me in resolving this issue.Thanks in advance..

AndroidOptimist
  • 1,419
  • 3
  • 23
  • 38

2 Answers2

0

You should add overridePendingTransition(R.anim.fade_in, R.anim.fade_out); in your second Activity. For example :

public class SecondActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Activity open animation
       overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
       setContentView(R.layout.activity_fragment_container);
   }

   @Override
   public void onPause() {
       super.onPause();
       // Activity closing animation
       overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
   }
}

Doing this you can control your Activity's start and close animation.

hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • Thanks for ur ans.. i edited my que.. my problem is animation is not working on real device.. – AndroidOptimist Nov 01 '13 at 09:27
  • Did you run my code? I am using this piece of code in lots of apps and I'm pretty sure it is working. Update your code with the one you are using now. – hardartcore Nov 01 '13 at 09:30
  • am using various cases in single click listener na, can i mention all those in onpause event together ah? – AndroidOptimist Nov 01 '13 at 09:40
  • As I can see you are using the same animations in onClick, it doesn't matter which button you are clicking. So just override your pending transaction in `Activity1` and `Activity2` like I posted and it will be working. – hardartcore Nov 01 '13 at 09:43
0

I had the same problem. Then i find decision. You should enable animation on your phone. Go to : Settings > Display > Animation(Path can vary from device to device). Enable animation here. Hope this helps.

Anatol
  • 941
  • 1
  • 7
  • 13