Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transition android fragment slide up

I have a fragment that is to replace another fragment. I want to specify the animation. But the animation is ignored.

transaction.replace(R.id.my_fragment, newFrag); transaction.addToBackStack(null); transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up); 

slide_in_up

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="@android:integer/config_longAnimTime"     android:fromYDelta="0%p"     android:toYDelta="100%p" /> 

slide_out_up

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="@android:integer/config_longAnimTime"     android:fromYDelta="100%p"     android:toYDelta="0%p" /> 

All I am really trying to achieve is for the new fragment to slide in from the bottom. My animations are ignored. What is the code missing?

like image 369
user3093402 Avatar asked Dec 17 '13 00:12

user3093402


2 Answers

It's been some time since this question was asked but here is an answer for other people coming here:

e1da is right insofar as that setCustomAnimation() call has to be called before replace(). Otherwise the animation will not show.
The second problem is that you probably are using native fragments that cannot be animated with the view animations.

Use the following files:

slide_in_up.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:fillAfter="true" >     <objectAnimator         android:duration="500"         android:propertyName="y"         android:valueFrom="1280"         android:valueTo="0"         android:valueType="floatType" /> </set> 

slide_out_up.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:fillAfter="true" >     <objectAnimator         android:duration="500"         android:propertyName="y"         android:valueFrom="0"         android:valueTo="-1280"         android:valueType="floatType" /> </set> 

A little explanation:
You have to differentiate between view animation for support fragments on one hand and property animation for native fragments on the other hand.

View animation:
Is the pre-android 3.0 way to animate views. Sample code for this is the slide_in.xml and slide_up.xml by user3093402

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromYDelta="0%p" android:toYDelta="100%p" /> 

It is worth mentioning you cannot animate fragments with view animation. The exception to this are fragments from the support library (android.support.v4.app.Fragment).

Property animation
This is the way to animate objects after android 3.0. It is also declared as .xml files but makes use of the "valueAnimator" tag (objectAnimator extends valueAnimator). Examples are in the answer to the question. This is the way how native fragments (android.app.Fragment) can be animated.

See also:

  • http://developer.android.com/guide/topics/graphics/overview.html
  • swap fragment in an activity via animation

Hope this helps,
Kai

EDIT: As pointed out by Raphael Royer-Rivard the fixed screen size is bad practice. It would be better to use a constant from the OS like in getWindowManager().getDefaultDisplay().getMetrics(metrics).xdpi (see DisplayMetrics). But I haven't done any Android development for some time so I don't know which one.

like image 45
Kaschwenk Avatar answered Oct 01 '22 06:10

Kaschwenk


transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up); transaction.addToBackStack(null); transaction.replace(R.id.my_fragment, newFrag); 

slide_in_up

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="@android:integer/config_longAnimTime"     android:fromYDelta="100%p"     android:toYDelta="0%p" /> 

slide_out_up

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="@android:integer/config_longAnimTime"     android:fromYDelta="0%p"     android:toYDelta="-100%p" /> 
like image 127
Tyler Davis Avatar answered Oct 01 '22 06:10

Tyler Davis