2

getting this error when animating Fragment Transaction.

Fragment A xml :

<FrameLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:background="#009688">

<RelativeLayout
    android:layout_weight="0.5"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/linear">

    <TextView
        android:layout_marginTop="35dp"
        android:id="@+id/mission"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="MISSION"
        android:textColor="#ffffff"
        android:textSize="70dp"
        android:gravity="center_horizontal"/>

    <TextView
        android:layout_below="@+id/mission"
        android:id="@+id/notimpossible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="NOT IMPOSSIBLE"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:textSize="35dp"
        android:gravity="center_horizontal"/>

</RelativeLayout>

<RelativeLayout
    android:padding="10dp"
    android:layout_weight="0.5"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/linear">

    <TextView
        android:id="@+id/random_main"
        android:textSize="20dp"
        android:textColor="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>


<TextView
    android:id="@+id/ahead"
    android:clickable="true"
    android:layout_marginBottom="35dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Go Ahead! "
    android:textSize="23dp"
    android:textColor="@drawable/text_focus_white"
    android:layout_marginRight="8dp"
    android:layout_gravity="right"/>


</LinearLayout>
</FrameLayout>

Fragment B xml :

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cddc39">

Code for tranaction from fragment A to fragment B:

    fragB frag = new fragB();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.addToBackStack("wTF");
    ft.setCustomAnimations(R.anim.slide_out_to_right, R.anim.slide_in_from_right);
    ft.replace(R.id.main,fragB);
    ft.commit();

</FrameLayout>

And here are the animation xmls

Slide_in_from_right :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
  <translate
    android:startOffset="500"
    android:fromXDelta="100%" android:toXDelta="0%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="700" />
</set>

Here is slide_out_to_right:

<?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:fromYDelta="0%" android:toYDelta="0%"
    android:duration="700" />
</set>
Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36

1 Answers1

8

No, you can use translate for Activities but not for fragment. You need to use objectAnimator for fragment.

Look at the 1st answer in this question, its working :

Android FragmentTransaction Custom Animation (Unknown Animator Name: Translate)

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129