I have two activities and I want that when the user touches a button on the first activity, the new activity slides in from the left and moves to the right while the first activity does the same, it moves to the right and slides out, so it would give an effect in which the new activity pushes the old one to the right and replaces it.
In order to do that, I have written the following XMLs:
In animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="1250" />
</set>
Out animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:duration="1250" />
</set>
I call the overridePendingTransition(R.anim.anim_in,R.anim.anim_out); function in the onCreate method of the new activity. In the resulting effect, the new activity moves from the left to right correctly, but the first, older activity moves into the opposite direction; it moves to the left. I want to revert the moving direction of this first activity. How can I do that, is there a XML property which serves to this purpose?
Change
android:toXDelta="-100%"
to
android:toXDelta="100%"
in the out animation.
Related
I have a navigation bar at the bottom with icons. When I click on an icon a new activity starts with intent.
context.startActivity(intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));
Like this.
If I test it on my Android Pixel emulator the animation is always from bottom to top when a new activity starts.
If I test it on my physical android device (Android 7.1) the animation is always from right to left when a new activity starts. When I close an activity the animation goes from left to right away.
I want always this slide animation (like on my physical device). What do I have to do?
I added
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
In the onCreate() of the activites, so I have the slide when the activity starts, but no slide animation when the activity close.
Thanks for your help
you'll need 2 new animation files in your anim folder and some code in the Activity which you want to close with the animation, add changes to onBackPressed method
First the animation files: 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:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</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>
And in Activity do the following:
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
PS Also be aware that even if your code is okay, your phone might have animation turned off
I have an activity and another activity.
I want my first activity to end when I slide up the screen. The animation should be like the activity is sliding up too. Like the notification screen.
Is that possible? I have done many Google searches before posting this question, but could not get anything.
P.S - I don't want this to be seen as a casual question since there is no code shown. I just need some point to start and I am completely baffled.
make an anim folder in res->
Make an xml file in anim folder slide_up_info.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
slide_down_info.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%p" />
</set>
no_change.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="0" android:duration="500"/>
</set>
Now when you want to activity up then write below code
Intent intent_info = new Intent(MainActivity.this,ToActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up_info,R.anim.no_change);
For down Activity animation
Intent intent_home=new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent_home);
overridePendingTransition(R.anim.no_change,R.anim.slide_down_info);
Animations can be introduced to Activity Launch using OverridePendingTransition. Take a look at this example where they have showed a sample code. Animation between activities
To end an activity, you can call finish() on the first activity.
The know about the slideup, you can check onFling from GestureDetector or onTouch from OnTouchListener
You can also do it this way : in your animation, override the onAnimationEnd method, and use startActivity in it to launch your new activity after the end of the animation
I need to start an activity in an animated way..can anyone help me?
Creating an intent and starting an activity in normal way will show new activity.I need to start it from one side,say left side..how to animate it near creating intent..
Use the following:
this.overridePendingTransition(R.anim.slidein_left, R.anim.slideout_right);
Where R.anim.* are Animation XML files in your /res/anim/ folder.
The following is an example of my slidein_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" />
</set>
And slideout_right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
What this will do is slide both activities to the left, making the new activity slide in from the left, pushing the old activity out to the right.
Also, as stated by #njzk2, please attempt to make an effort yourself before asking questions, and provide us with things that you may have already tried.
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) 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.
Use this code:
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);
}
I've got a layout much like the one below. Currently when the back button is pressed the red linear layout's visibility is set to gone. However, I'd like it to "slide" up off of the page instead. How would I do this?
You need to use animations. here is the top in/out animations:
In Top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="300"/>
</set>
Out Top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="600"/>
</set>
Then in your activity get the view and apply an animation to it like this:
This are type Animation.
mSlideInTop = AnimationUtils.loadAnimation(this, R.anim.slide_in_top);
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
and call them with this code:
header.startAnimation(mSlideOutTop);
header.setVisibility(View.INVISIBLE);
Here header is a LinearLayout wrapping my views. same thing if you want to make it slide in. just add the slide in animation and make the view visible.