Make basic Android fragments

Flexible UI construction

Today, I wanted to show you how to make a simple UI with fragments (in Java)!

So when designing applications that support a widescreen area, it is useful to reuse fragments as different layouts that are configured in a form suitable for the user experience based on the available screen space.

For example, on a palm-sized device, it may be appropriate to display only one fragment as one window of user-interface for one operation. On the other hand, on a tablet where you can display lots of information on a big screen, you would like to keep several fragments next to each other.

android fragments

Figure 1: Display two fragments in different configurations with different activities using the same activity. In large screen, arrange the fragments of therapy, in a palm-sized device only show one fragment once, switch the fragmented display by user operation. The FragmentManager class provides methods to dynamically add, delete, and replace fragments on runtime activities.


Add Fragment to Activity at runtime

Instead of defining fragments for activity in the layout file using the ” element as in the previous lesson, you can add Fragment to the activity while the activity is running. This will be necessary if you change fragments throughout the lifecycle of the activity.

To perform transaction operations such as adding and deleting fragments, create a FragmentTansaction using FragmentManager, and provide the addition, deletion, and replacement of the API to realize transactions with other fragments.

If an activity removes a fragment or permits substitution, you should add an initial fragment to the activity’s onCreate () method.

An important rule when dealing with fragments (especially when doing additions at runtime), fragments must have a View container inside the layout in which the fragment layout resides.


<FrameLayout xmlns:android=""
    android:layout_height="match_parent" />


In the activity, we call getSupportFragmentManager () to get the FragmentManager using the Support Library API. Then we call beginTransaction () to create a FragmentTransaction and call add () to add a fragment. You can use the same FragmentTransaction to handle fragment transactions in parallel in an activity. If you are ready to change, you must call commit (). For example, to add a fragment to the previous layout:


import android.os.Bundle;

public class MainActivity extends FragmentActivity {
    public void onCreate(Bundle savedInstanceState) {

        // Check that the activity is using the layout version with
        if (findViewById( != null) {
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
            // Create a new Fragment to be placed in the activity layout
            FirstFragment firstFragment = new FirstFragment();
            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            // Add the fragment to the 'container_frag' FrameLayout
                    .add(, firstFragment).commit();

Fragments are added to the FrameLayout container at run time instead of defining the @ element in the layout of the activity. Activities can also delete fragments or replace them with another fragment.

Replace with another fragment
The procedure to replace fragments is the same as adding. However, instead of the add () method we use the replace () method.

Noting that fragment transactions are working, it is often appropriate for users to make changes such as “<” marking operations or replacing or deleting fragments by “back”. To do “<” mark operation through a fragment transaction, you must call addToBackStack () before committing the fragment transaction.

An example of replacing a fragment with another fragment.

SampleFragment newFragment = new SampleFragment();
Bundle args = new Bundle();
args.putInt(SampleFragment.ARG_POSITION, position);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(, newFragment);

// Commit the transaction

The addToBackStack () method has an arbitrary string argument that uniquely specifies the transaction. This name is not necessary unless you do more advanced fragment operations using FragmentManager.BackStackEntry API.

Well, that is the basic sample for the fragments in android. I will be making a tutorial about making fragments with kotlin soon!