I want to display one activity to another from bottom to top animation using Intent.
Given me some example for that like below
Intent i2 = new Intent(main.this, test.class);
startActivity(i2);
with animation bottom to top.
Define an animation in res/anim/slide_in_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p" android:toYDelta="0%p"
android:duration="#android:integer/config_longAnimTime"/>
and another at res/anim/slide_out_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p" android:toYDelta="-100%p"
android:duration="#android:integer/config_longAnimTime"/>
Then apply these after to call startActivity:
Intent i2 = new Intent(main.this, test.class);
startActivity(i2);
overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
There an introduction to Android animation here which may help you further.
As i googled sliding up transition and landed here but Mark answer is incomplete without transition involved on coming back to the same activity.
Overide finish in activity
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_from_top,R.anim.slide_in_top);
}
finish()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAfterTransition();
}else finish();
slide_from_top.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="-100%p" android:toYDelta="0%p"
android:duration="#android:integer/config_shortAnimTime"/>
slide_in_top.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p" android:toYDelta="100%p"
android:duration="#android:integer/config_shortAnimTime"/>
First : animate the layout of your activity 1 ,,
Second : at the end of this aniamtion , Start your second Activity , and animate her Content Layout
Use TranslateAnimation(int fromX,int toX, int fromY , int toY) ;
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
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
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 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 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.