How to add a fragment to an activity? -via XML

You know that, in android, an activity has a lifecycle associated with them, activities receive their own input events. Like an activity, a fragment too has a lifecycle associated with them and receives its own input events. So, You can call a fragment a sub-activity. You can add a fragment to the activity or, remove a fragment from the activity during runtime. You can have multiple fragments in an activity.

There are two ways to add a fragment to an activity. One method is to add the fragment via XML and the second method is adding a fragment programmatically.

In this post, I will cover the first method of adding a fragment to an activity. I will cover the second method in my next post.

How to add fragments to activity in android?

First set up the project with an activity.

Now create a layout file for the fragment. Like this-

fragment_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5BDDE1"
    tools:context=".MainActivityFragment">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Hi there! I'm a fragment"
        android:textColor="#FAF6F6"
        android:textSize="30sp" />

</FrameLayout>

This is a simple layout with a TextView inside a FrameLayout. We will inflate this layout inside a class that extends Fragment.

Now, create a class that extends Fragment and override its methods. Like this –

MainActivityFragment.java

package com.example.fragment_example;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivityFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main_activity, container, false);
    }
}


You inflate the layout for the fragment inside the onCreateView() method. This method returns the inflated layout. See the code above. I have inflated the layout that I created earlier inside the onCreateView() method.

Now, to add this fragment to the activity change the activity_main.xml file like this –

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <fragment
        android:id="@+id/fragment"
        android:name="com.example.fragment_example.MainActivityFragment"
        android:layout_width="369dp"
        android:layout_height="192dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

To add the fragment to an activity we use the <Fragment> element. Here android:name attribute is important because it specifies the class name of the Fragment to instantiate.

MainActivity.java

package com.example.fragment_example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Output:

That’s how you add a fragment to an activity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: