5

I am trying to add animation to a fragment appearing on button click, from bottom.

Fragment Layout

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="@color/wallet_holo_blue_light"
        android:layout_gravity="center"
        android:text="This is a fragmentt layout"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Slide up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime">

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="50.0%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toYDelta="30" />
</set>

Calling it here:

public void onClick(View view) {

           if(view == button ){

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FragmentOnMap hello = new FragmentOnMap();

        fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
        fragmentTransaction.setCustomAnimations(R.animator.slide_up,0);
        fragmentTransaction.commit();
    }


}

Android Fragments and animation shows how to slide up AND down, I just the fragment to animate up. Hence my question, of the second argument of setcustomanimation() function

I tried using fragmentTransaction.setCustomAnimations() just before commit, but didn't help.

It does appear at the bottom, but there is no transition effect. Any guidance would be useful. Thank you

Community
  • 1
  • 1
AIS
  • 297
  • 1
  • 4
  • 16
  • 2
    Possible duplicate of [Android Fragments and animation](http://stackoverflow.com/questions/4817900/android-fragments-and-animation) – jakubbialkowski Oct 23 '16 at 17:45

1 Answers1

2

You should setCustomAnimation before add, that's all.

also, you should have a problem with your code because you should use the support library of the fragment (v4) instead of app, and call getSupportFragmentManager and fix all parts of code when you use fragment, and you don't use the support library.

If you don't want to change this, you can take this code for the slide_up animation:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="translationY"
    android:valueType="floatType"
    android:valueFrom="1280"
    android:valueTo="0"
    android:duration="@android:integer/config_mediumAnimTime"/>
T.S
  • 911
  • 8
  • 24
  • What will be the second argument of setcustomanimation? setCustomAnimation(R.id.slideup,?) – AIS Oct 23 '16 at 18:34
  • fragmentTransaction.setCustomAnimations(R.animator.slide_up, 0); or R.anim.slide_up if you use the support library. – T.S Oct 23 '16 at 18:47
  • See my edited code. Didn't animate. Used your slide_up. The fragment just appears. There is no sliding effect. – AIS Oct 23 '16 at 18:54
  • as i said,You should setCustomAnimation before add and if it doesn't work for you, then change the xml file to the code i added or follow the changes i said in the answer. – T.S Oct 23 '16 at 18:57
  • Yes, that worked. My bad. I was adding it after add. – AIS Oct 23 '16 at 19:02
  • But why user ObjectAnimator? Not translation – AIS Oct 23 '16 at 19:03
  • I think the xml you write only works with the support library and the code i added works with the app library that you used throgh your code. – T.S Oct 23 '16 at 21:20
  • what does valueFrom=1280 means here ? can we change it ? How do we decide this value ? – xaif Apr 05 '20 at 08:03