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
Related
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);
I have an Activity which has a BottomNavigationView.It has 2 items(Fragments).The first fragment has to enter from left and exit to the left.Similarly the second fragment has to enter from the right and exit to the right.Both the fragments enter correctly, but exits the opposite ways.Have a look here:
enter_from_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:shareInterpolator="false">
<translate
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="-100%"
android:toXDelta="0%"
>
</translate>
</set>
exit_to_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:shareInterpolator="false"
>
<translate
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%"
android:toXDelta="-100%"
>
</translate>
</set>
enter_from_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:shareInterpolator="false">
<translate
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="100%"
android:toXDelta="0%"
>
</translate>
</set>
exit_to_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:shareInterpolator="false">
<translate
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%"
android:toXDelta="100%">
</translate>
</set>
Code:(Exit animations work oppositely for both fragments)
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.sendSms:
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_left,R.anim.exit_to_left);
transaction.replace(R.id.fragcontainer,new SendSmsFragment());
transaction.commit();
return true;
case R.id.receiveSms:
FragmentTransaction transaction1=getSupportFragmentManager().beginTransaction();
transaction1.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_right);
transaction1.replace(R.id.fragcontainer,new ReceiveSmsFragment());
transaction1.commit();
return true;
}
return false;
}
};
Try exit_to_right: transaction.setCustomAnimations(R.anim.enter_from_left,R.anim.exit_to_right);
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'm trying to create an Android app, and switch between activities with a slide, when the previous activity slides out to the right, and the new activity comes in the left.
I can do somethibg like this, with:
Intent intent = new Intent(getContext(), NextActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_ltr, R.anim.out_ltr);
Where the in_ltr.xml is:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="700"
/>
</set>
And the out_ltr.xml is:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
/>
</set>
It's almost perfect, but a small black gap appers between the two activity during the animation. You can see it here on YouTube.
I've tried anything I found in the Google in the last hour, but nothing seems to remove that gap. Is it possible to remove that gap?
Thanks!
Try to set Alpha with translate effect....
Try This...
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:interpolator/accelerate_cubic">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="400" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.3"
android:duration="500" />
</set>
Add this two file in res/anim folder.
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>
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>
And write the following code in onCreate() method of next activity that you pass through the Intent.
overridePendingTransition(r.anim.slide_in, R.anim.slide_out);
hope it will remove your space
Try this: create a RootActivity.java and extend it in your activity.
import android.app.Activity;
import android.os.Bundle;
public class RootActivity extends Activity {
int onStartCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onStartCount = 1;
if (savedInstanceState == null) // 1st time
{
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
} else // already created so reverse animation
{
onStartCount = 2;
}
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if (onStartCount > 1) {
this.overridePendingTransition(R.anim.anim_slide_in_right,
R.anim.anim_slide_out_right);
} else if (onStartCount == 1) {
onStartCount++;
}
}
}
add these animations to anim folder:
anim_slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
</set>
anim_slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
</set>
anim_slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="100%" >
</translate>
</set>
anim_slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="-100%" >
</translate>
</set>
Try to add below item into your AppTheme
<item name="android:windowIsTranslucent">true</item>
Sorry guys, I was very, very stupid.
I changed the duration to the same amount of time, and the gap disappeared.
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();
}