Im trying to add a sliding animation when entering and exiting an activity but it only works when the phones back button is pressed. When pressing the up button in the toolbar or a separate button it doesn't work.
I tried adding the overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right); line to the onPause() method as well as the finish() method and the buttons OnClick method but it doesn't work :/
no_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="0"
android:fromXDelta="0"
android:toXDelta="0" />
</set>
slide_out_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_longAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
Java:
public void button(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}
#Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}
#Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}
You can call onBackPressed() when pressing the up button in the toolbar
Set toolbar Navigation click listener:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); //close activity
overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
}
});
This will surely work!
Related
I would like to add an animation before the opening of a new Android Empty Activity. Something like a chroma-keyed video played on top of the current activity and at the end of it, the secondary activity opens up.
You create a splash activity that includes your animation, and you implement an AnimationListener. Inside the method onAnimationEnd() you create the intent that takes you to the second activity. Don't forget to set the splash activity as the Launcher activity on your manifest.
animationObject.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent (SplashActivity.this, MainActivity.class);
startActivity(intent); }
#Override
public void onAnimationRepeat(Animation animation) {
}
});
EDIT: if you want to play a video with media player instead you use a playback listener and run the same intent from onCompletion()
After your startActivity method use the overridePendingTranistion and put the animations in it
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(ActivityA.this, ActivityB.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});
The xml animations are as follows
enter.xml:
<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>
exit.xml:
<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 can't figure out why when I click my "toactility" button on the following code, the overridePendingTransition doesn't respond at all and instead I receive a default fade in style animation. Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_navi);
Button ToMainActivity = (Button)findViewById(R.id.toactivity);
ToMainActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(startIntent);
overridePendingTransition(0, R.anim.navigation_slide_left);
finish();
}
});
}
I've tried placing finish(); in various places, above startActivity(startIntent); and below it. overridePendingTransition is ignored.
However, on the Android hard back pressed button the overridePendingTransition works as expected. Here's the code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void finish() {
super.finish();
overridePendingTransition(0, R.anim.navigation_slide_left);
}
Can't figure out why when the overridePendingTransition code won't work below or above startActivity(startIntent);
How do I get it to work?
From here where I have answered, use like this:
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_out_left);
Then in res->anim folder:
enter_from_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
</set>
exit_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0%"
android:toXDelta="-100%" >
</translate>
</set>
How can i impliment slide etc.. animation transition when i open new activity by listview item click ?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
// global string to class
selectedValue = String.valueOf(parent.getItemAtPosition(position));
if (selectedValue.equals("item1")) {
// ^^^ use any item value here you want
Intent myIntent = new Intent(view.getContext(), activity1.class);
startActivityForResult(myIntent,0);
}
else if (selectedValue.equals("item2")) {
Intent myIntent = new Intent(view.getContext(), aactivity4.class);
startActivityForResult(myIntent,0);
}
}
});
santoash kumar answer is correct. If you struggle on R.anim.layout resource here an animation code work like how other chat application work(slide animation) when you click on the listview item.
Add those into your anim resource
R.anim.push_left_in
<?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="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_left_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-20%" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-20%" android:toXDelta="0" android:duration="#android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_out
<?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="#android:integer/config_mediumAnimTime"/>
</set>
In the activity1 or any activity you will like to perform this animation while it launch,add this code after your set setContentView()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//rest of your code include setContentView();
}
Now you will find a problem when you try to navigation back to your listView activity either with pressing the back button or click on the view which represent the back button the animation still seem to be the default animation so do this when you try to end your current activity.
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
For the back button pressed use this code.
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
}
If the animation I provide doesn't suit the animation you desire you can try to make your own custom animation from this link.
startActivityForResult(myIntent,0);
overridePendingTransition(R.anim.hold, R.anim.fade_in);
hold and fade in will be animation xmls
I am using animations when entering or exiting the activity,entering to an activity animation works fine but exit animation does not work well. I start exit animation when i press back button.What happens is it first start enter animation for current activity then show the last activity what i want a simple exit animation on back button press.
Slide_out.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%p"
android:toXDelta="-100%p" >
</translate>
Slide_in.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" >
</translate>
On Action bar back button pressed
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.home:
finish();
overridePendingTransition(R.anim.slide_out, R.anim.slide_in);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Firstly create one more animation i.e nothing.xml in your anim folder
nothing.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="0%" >
</translate>
here is your slide_in.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%"
android:toXDelta="0%" >
</translate>
and slide_out.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%" >
</translate>
Now call your NewActivity like this
startActivity(new Intent(CurrentActivity.this, NewActivity.class));
overridePendingTransition(R.anim.slide_in, R.anim.nothing);
and then on your back button click do this
finish();
overridePendingTransition(R.anim.nothing, R.anim.slide_out);
I used
slide_in.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%"
android:toXDelta="0%" >
</translate>
slide_out.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%" >
</translate>
slide_enter.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%"
android:toXDelta="0%" >
</translate>
slide_exit.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%" >
</translate>
Code
Intent i=new Intent(Authentication.this,Login.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
To previous
finish();
overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
Add animation in onBackPressed, it will show the animation while clicking the back button.
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
I solved it by overriding the back button behavior.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
}
I've done something similiar and it works fine, you can change animate() with startAnimation(your_xml), you will also have to make activity background transparent:
<item name="android:windowBackground">#android:color/transparent</item>
This activity will slide down, and the MainActivity will be visible at the moment of sliding because of transparent background.
EDIT - with toolbar back button:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainlinear.animate()
.translationY(ScUtils.getScreenHeight(getApplicationContext()))
.setDuration(210)
.setInterpolator(new AccelerateInterpolator())
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
ThemeActivity.super.onBackPressed();
overridePendingTransition(0, 0);
}
}).start();
}
});
For the Activity enter animation use overridePendingTransition(R.anim.slide_out, R.anim.slide_in); in the onCreate(...) function.
For the exit animation place the same call in onPause(...).
I had a back/home button on my action bar which was not picking up the slide animation by overriding onBackPressed or finish. So I had to add this snippet from here. If using the same slide_enter and slide_exit as above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
// back button
case android.R.id.home:
finish();
overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
return true;
}
return super.onOptionsItemSelected(item);
}
In case somebody finds it useful.
When you navigate to or from an Activity, pop animations are not applied automatically.
Once the animation is added to the navigation XML, it must be added to the target activity an override of Activity.finish() with applyPopAnimationsToPendingTransition to have the pop exit animation.
override fun finish() {
super.finish()
ActivityNavigator.applyPopAnimationsToPendingTransition(this)
}
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..
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.
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.