Android Fragment Tutorial

Android Fragment is a modular section of an activity which enable to combine multiple fragment into one activity. Each fragment activity have different layout, behavior and life cycle. Fragment should always embedded in the activity and it will affect by the host activity life cycle. For example, when the activity is destroyed then all the fragments will behave same as the activity.

In this tutorial, I will discuss on how to implement the android fragment in the android project. I will briefly explain how its work in the activity.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “FragmentExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Create a new layout file

Go to res > layout, right click and create a new layout resource file for fragment_one.

Edit fragment_one.xml layout

Open your fragment_one.xml and edit the design to the following code. I put the textview for displaying which fragment is opening and the button is to switch to fragment two.

Create a new java class

Go to java > your package name right click and create new java class for fragment one. For example, I name the class as PlaceHolderFragment.

Edit PlaceHolderFragment.java class

This class should extends Fragment so it will categories as a Fragment. The inflator is an Object that can inflate any views in the fragment.

Repeat Step 2 to create fragment two

Repeat the step 2. Go to res > layout, right click and create a new layout resource file for fragment_two.

Edit fragment_two.xml layout

Open your fragment_two.xml and edit the design to the following code. The button is to go back to the first fragment.

Create a new java class

Go to java > your package name right click and create new java class for fragment two. For example, I name the class as PlaceHolder2Fragment.

Edit PlaceHolder2Fragment.java class

Edit activity_main.xml

In your activity_main.xml edit the relativelayout to framelayout.

Edit MainActivity.java class

For your MainActivity class and insert the following code.

getSupportFragmentManager()

– Return the fragmentmanager to interacting with fragments assoicated with this activity.

beginTransaction()

– Start the edit operations on the fragments in the fragmentmanager.

Run your project

Now, you can start your android project and try the fragment in your own device.

(Android Fragment)

Source Code

(Visited 339 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

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