I can't figure out why overridePendingTransition won't work with custom animtions.
In MainActivity:
private void goToDetails() {
Intent intent = new Intent(requireContext(), DetailsActivity.class);
startActivity(intent);
}
In DetailsActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
The above code works as expected. BUT, if I copy the source files from android.R.anim.slide_in_left & slide_out_right, then change this line to:
overridePendingTransition(R.anim.my_slide_in_left, R.anim.my_slide_out_right);
Then the animation doesn't work - the default slide up animation happens. I didn't change the code in the layout files, I literally copy/pasted them so I could tweak them later.
/res/anim/my_slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="#android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
</set>
/res/anim/my_slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
android:duration="#android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="#android:integer/config_mediumAnimTime" />
</set>
MainActivity click event:
startActivity(new Intent(getContext(), NewReportActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
DetailActivity onBackPressed:
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
Related
i have 5 to 6 activities in my application I have added left to right and right to left animation on activity start and on finish activity but its not working properly when I click for new activity its working fine it start from right to left but when i press back button its also start from right to left. but when I press hardware back button its work from left to right. here is my code
right to left animation
leftin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>
leftout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"/>
rightin.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"/>
rightout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"/>
here is code oncreate activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub__category);
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
and here is code of my_back button
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
});
and here is code of my onbackpress override method.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent myint = new Intent(Sub_Category_Activity.this,Home.class);
startActivity(myint);
overridePendingTransition(R.anim.right_in, R.anim.right_out);
finish();
}
here I am facing problem is that when I press hardware back button its work fine but when i press back button of my UI it not start from left to right but start new activity form right to left
Use this code:
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
Slide in 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="800" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="800" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
Slide out 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="800" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
write overridePendingTransition(R.anim.right_in, R.anim.right_out);
after finish(); method.
I don't think you need to invoke overridependingtransitions() method in onCreate().
hi here is how i achieved this animation. i know my thinking of achieving this is not professional way but finally i achieved it by my way.
here is i have activity A,B,C so on activity B i check that is it clicked from Activity A or C and according to that i used if condition and implement animation some way like below code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
editor = pref.edit();
String fromhome =pref.getString("from_home_list_adapter","");
if(fromhome.equals("yes"))
{
editor.putString("from_home_list_adapter","no");
editor.commit();
overridePendingTransition(R.anim.left_in, R.anim.left_out);
}
again i say this is not professional way to handle animation but its works
Using the overridePendingTransition method, how would I animate the current (finished) activity down and out of the way, revealing the newly loaded (startActivity) activity behind it?
slide_out_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#anim/full_screen_modal_decelerate_interpolator">
<translate android:fromYDelta="0%" android:toYDelta="100%" android:fillAfter="true"
android:duration="300"/>
</set>
onclick:
#Override
public void onClick(View v) {
finish();
Intent intent = new Intent(context, TestActivity.class);
startActivity(intent);
overridePendingTransition(0, R.anim.slide_out_bottom);
}
Use zAdjustment to make it so that the exiting activity is on top:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#anim/full_screen_modal_decelerate_interpolator"
android:zAdjustment="top">
<translate
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="300"/>
</set>
I want my launcher activity to slide in from the right to the left when it is opened or returned to from a previous activity. It currently slides from the right to the left when it switches to another activity, but not when it is opened or the back button is pressed.
Here is the relevant XML for the style that is applied to my launcher activity:
styles.xml
<item name="android:windowContentTransitions">true</item>
<item name="android:windowEnterTransition">#android:transition/slide_left</item>
<item name="android:windowExitTransition">#android:transition/slide_left</item>
<item name="android:windowReenterTransition">#android:transition/slide_right</item>
<item name="android:windowReturnTransition">#android:transition/slide_left</item>
My activity moves to the next activity when a button is clicked. This is my onClickListener:
Button register = (Button)findViewById(R.id.registerBtn);
register.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i, ActivityOptions.makeSceneTransitionAnimation(LoginActivity.this).toBundle());
}
});
I believe the ActivityOptions.makeSceneTransitionAnimation method may have something to do with why the exit transition works, but how do I apply this for an enter transition?
If your activity is AppCompatActivity, try this:
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this).toBundle();
ActivityCompat.startActivity(RegisterActivity.this, intent, bundle);
You can use this to start a new activity with transition
startActivity(new Intent(this, NewActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
Create file res/anim/slide_in_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="1000" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
Create file res/anim/slide_out_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="2000" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
You can make your activity slide accordingly as per requirements using this method.
When I instanciate a new Activity is use a custom animation like this:
Intent registerIntent = new Intent();
registerIntent.setClassName(getPackageName(), getPackageName() + ".activity.RegisterActivity");
startActivity(registerIntent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
This way the current/old activity slides out to the left, while the new activity slides in from the right.
When in the new RegisterActivity I use following code to handle the animation when the user uses the "Back" button:
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
Now the current activity leaves to the right and the old activity (from before) comes back in from the left.
I also tried to handle the "Up" Navigation like this, but with no result:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
return super.onOptionsItemSelected(item);
}
How would i use a custom animation on "Up" navigation? Any suggestions on how to better use animations?
EDIT:
slide_in_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500" />
</set>
slide_out_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500" />
</set>
I changed the following: Now it works:
Old:
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue)));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
New:
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue)));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
If I understood... I think that this resolve.
Intent registerIntent = new Intent();
registerIntent.setClassName(getPackageName(), getPackageName() + ".activity.RegisterActivity");
startActivity(registerIntent);
overridePendingTransition(
R.anim.animation_in+right_to_left,
R.anim.animation_out_right_to_left);
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation_in_left_to_right,
R.anim.animation_out_left_to_right);
}
animation_in_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:duration="500"/>
</set>
animation_out_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="0%" android:toXDelta="200%"
android:duration="500"/>
</set>
animation_in_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:fromXDelta="200%" android:toXDelta="0%"
android:duration="500"/>
</set>
animation_out_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:fromXDelta="0" android:toXDelta="-100%"
android:duration="500" />
</set>
you use the same logic to UP navigation
I am trying to do a slide animation between two Activities when then one starts the other,
public void onClick(View view) {
Intent intent = new Intent(TestAppActivity.this, SecondActivity.class);
startActivityForResult(intent, 1);
TestAppActivity.this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
finish();
}
There is no animation at all. The xmls are, for enter:
<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="3000" />
And for leave:
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="3000" />
I can see what is gong wrong here. Using Android 2.3.3. Thanks.
Put the overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave); after finish();.
To do an animation where the first activity go to the left and the second activity enters from the right :
slide_out_left.xml :
<?xml version="1.0" encoding="utf-8"?>
<!--
Animation : Perform animation : Out - Direction : Left
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
slide_in_right.xml :
<?xml version="1.0" encoding="utf-8"?>
<!--
Animation : Perform animation : In - Direction : Right
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
Note : you can change android:duration if you want.
And you have to add this code :
public void onClick(View view) {
Intent intent = new Intent(TestAppActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
finish();
}