Android Fragment → Android Fragment – Camposha

Android Fragment

Android Fragment

Android Fragment

Android Fragment Tutorial

 

Quick Android Fragment Examples and HowTo’s

1. How to Replace a Fragment Using FragmentTransaction

Yeah, we can easily replace a fragment via FragmentTransaction.

But first we need a placeholder where the Fragment will be inserted within our activity.

So for example if you have a layout like activity_main.xml, then within that layout add a placeholder like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    
    android_id="@+id/drawer_layout"
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    android_clickable="true">
        ......
        <FrameLayout
            android_id="@+id/main_frame"
            android_layout_weight="1"
            android_layout_width="match_parent"
            android_layout_height="0dp">
            .....
</android.support.v4.widget.DrawerLayout>

So in our case we’ve used the framelayout, however you can use any layout.

All we need is a place where our Fragment will be rendered and FrameLayout is perfect for that.

Then we proceed to our java code.

Let’s create a method that will receive a Fragment object as a parameter.

We’ll then start by retrieving the FragmentManager using the getSupportFragmentManager() method of the activity.

Then calling the beginTransaction() of that FragmentManager will give us a FragmentTransaction instance.

Then we can use that FragmentTransaction to replace the FrameLayout we defined above with our Fragment using the replace() method.

Then we make sure we commit that Transaction.

    private void replaceFragmentAnimation(Fragment fragment) {

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.main_frame, fragment);
        transaction.commit();

    }
2. How to Replace a Fragment with Animation Transitions

You may want to show animations, or page transitions as you replace or navigate to Fragments. Normally FragmentTransaction class is what allows us replace Fragments and we’ve seen how to do that.

Now let’s see how to use animations as we transit to a Fragment.

First we need to define an animation resource file. In your res directory add another directory called anim.

Then create the following xml animation files in that anim directory;

slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Slide from left to right-->
<set 
    android_fillAfter="true">

    <translate
        android_duration="200"
        android_fromXDelta="-100%p"
        android_toXDelta="0"/>
</set>

slide_left_out.xml


<?xml version="1.0" encoding="utf-8"?>
<!--Slide out from right to left-->
<set >
    <translate
        android_duration="200"
        android_fromXDelta="0"
        android_toXDelta="-100%p"/>
</set>

slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Slide from right to left-->
<set 
    android_fillAfter="true">

    <translate
        android_duration="200"
        android_fromXDelta="100%p"
        android_toXDelta="0"/>
</set>

slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Slide out from left to right-->
<set >
    <translate
        android_duration="200"
        android_fromXDelta="0"
        android_toXDelta="100%p"/>
</set>

Then inside your activity as class fields have the following constants:

 Private final int SLIDE_FROM_LEFT_TO_RIGHT = 0 ; //Sliding the page from left to right 
 private final int SLIDE_FROM_RIGHT_TO_LEFT = 1 ; // Swing the page's animation from right to left   

Then within a method inside your activity:

    private void replaceFragment(Fragment fragment, int slideType) {

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        switch (slideType){
            case SLIDE_FROM_LEFT_TO_RIGHT:
                transaction.setCustomAnimations(R.anim.slide_left_in,R.anim.slide_right_out);
                break;
            case SLIDE_FROM_RIGHT_TO_LEFT:
                transaction.setCustomAnimations(R.anim.slide_right_in,R.anim.slide_left_out);
                break;
        }

        transaction.replace(R.id.main_frame, fragment);
        transaction.commit();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //maybe activate some buttons or something else
            }
        }, 200);
    }

You can see this time our method has received a Fragment as well as an integer to identify the type of sliding animation we want to make.

Then you can use that method like this:

    replaceFragment(orderFragment, SLIDE_FROM_LEFT_TO_RIGHT);

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Us
X