I am working on one demo application where I want to apply animation whenever app start any activity
. I wrote below code but this is for to animate the activity from left to right.
left_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
right_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
I am here starting an activity
like this
startActivity(new Intent(this, LoginActivity.class));
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
I want to achieve an animation right to left. How this can be done.
Thanks in advance.
overridePendingTransition
should be called in the "target" activity. For example: Going from Activity A -> B, you would put theoverridePendingTransition
call in theonCreate
of Activity B.Keep in mind, if the user has disabled animation on a system level, you can't force animations to show.
EDIT:
An example would look like this:
This is the perfect code for me Slideinleft
Slideinright
In Activity
Do these modifications to your animation files:
enter.xml:
exit.xml:
You'll have your second activity sliding in from right to the left.
For a better understanding on how to play around with the fromXDelta and toXDelta values for the animations, here is a very basic illustration on the values:
This way you can easily understand why you add android:fromXDelta="0%" and android:toXDelta="-100%" for your current activity. And this is because you want it to go from 0% to the -100% position.
[EDIT]
So if you want to open ActivityB from ActivityA you do the following(let's say you have a button):
Now, if you want to have the "backwards" animation of the first one, when you leave Activity B, you'll need 2 new animation files and some code in the ActivityB's onBackPressed method, like this:
First the animation files: left_to_right.xml:
right_to_left.xml:
And in ActivityB do the following:
Also if you have up navigation enabled, you'll have to add the animation in this case as well:
You enable UP navigation like this:
And this is how you handle the animation in this case too:
Also be aware that even if your code is okay, your phone might have animation turned off. To turn then on do the following:
Does this help?
Try this code, it's working for me
To slide from right to left
To slide from left to right