0

I am learning to develop android apps and I am currently trying to add a fragment by sliding it from a side. The fragment is added, but it just appears, without animation.

There is a button in FragmentOne, I attach an event to it in MainActivity so that when clicked FragmentTwo. The event is correctly intercepted

Can anybody spot what I am doing wrong?

MainActivity.java

public class MainActivity extends Activity
{
    LinearLayout ll;
    FragmentOne f1;
    Animation fadeIn;

    @Override
    protected void onCreate ( Bundle savedInstanceState )
    {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );

        f1 = new FragmentOne ();

        FragmentTransaction ft1 = getFragmentManager ().beginTransaction ();
        ft1.add ( R.id.topFl, f1 );
        ft1.commit ();
    }


    @Override
    protected void onStart ()
    {
        super.onStart ();

        Button b = f1.getAddFragmentButton ();
        b.setOnClickListener
        (
            new Button.OnClickListener () 
            {
                public void onClick ( View v ) 
                {
                    FragmentTransaction ft2 = getFragmentManager ().beginTransaction ();
                    ft2.setCustomAnimations ( R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_right, R.anim.exit_to_left );

                    ft2.addToBackStack ( null );

                    ft2.add ( R.id.bottomFl, new FragmentTwo () );
                    ft2.commit ();
                }
            }
        );
    }
}

enter_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="-100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="1700"/>
</set>

exit_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <objectAnimator
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="1700"/>
</set>
Dan
  • 530
  • 6
  • 22

1 Answers1

2

Use the support library fragments and instead of using getFragmentManager(), use getSupportFragmentManager().

It might sound counter-intuative, but it's better to use the support library components because any bugs (like this) are updated and fixed, whereas the platform components are tied to platform releases.

JiTHiN
  • 6,548
  • 5
  • 43
  • 69
JoeyJubb
  • 2,341
  • 1
  • 12
  • 19
  • Thank you so much for the quick reply! I did as instructed straight away, and I am now getting the following error: "java.lang.RuntimeException: Unknown animation name: objectAnimator". I realise it's because objectAnimator in the XML is more recent than the devices that the support library targets, fair enough. But I did a lot of searching and I can't find the right one. Would you happen know to what I should use instead? Even just a keyword which would make my search easier. – Dan Oct 24 '15 at 10:53
  • 1
    I'm not at my computer right now so I can't be exact, take a look at the android.R.anim.slide animations. They're supported by older devices. – JoeyJubb Oct 24 '15 at 11:28
  • That did the trick, thank you very much again. I couldn't find the substitute for objectAnimator because there isn't one :-) – Dan Oct 24 '15 at 11:39