top of page

Learn Android App Dev: Using Fragments in your Activity

Hey guys! I am Amey Lokhande. In this post, I'll be telling you what are fragments and how to use them in your App. Also I'll be developing an App which includes multiple fragments.

So, now you might be thinking what fragment means.

Fragments are portion of a User Interface. That means we can use Fragments inside our Android Activity. Also you can combine multiple fragments in single Activity and use them for different functions. Also you can reuse these fragments in multiple activities. Fragments are generally used to simplify the tasks in our Android Activity. Also it will reduce our code writing. Here is an example of Android Fragment.

I'll explain it. So here the names of the things represent one fragment and when you click on it, it will give its details. So the details are fragment 2. Now you might be thinking whether these are two different activities or the same. Well, these two fragments are accommodated in only one Fragment.

So, here are the steps:

1. First of all, lets design the layout of our App. So, drag two buttons which will be used for switching between the fragments. Now you need to create two new fragments.

2. For creating the fragments follow the screenshot. Create a blank fragment and give an appropriate name to your fragment. Similarly create two fragments.

3. Now Android Studio will automatically generate some code for you. We have to edit that code. Go to fragment_one.java and inside it keep only the OnCreateView method inside.

So your code will be somewhat look like:

Similarly, do this for fragment_two.java. So your app will somewhat look like:

4. Now we have done the design part. Now lets design the Java code.

5. Now to work with the fragments we need to import the Fragment Manager class. The Fragment Manager class helps us to deal between the transactions of the fragments.

6. So, in your Main_Activity.java, first of all, import the following libraries.

import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction;

7. Now, create a method for switching between these fragments.

8. Here we have created an if loop. This will help us to switch between the fragments. Create a new FragmentManager class instance and create a new FragmentTransaction. Replace the fragments in the fragment widget. You need to Commit every fragment after working with it.

9. Now mention this Method in the onClickmethod of the button click. Now we are done.

Here is the Java code:

package com.example.ameylokhande.usingfragments;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void switchfrag(View v){ Fragment frag; if (v == findViewById(R.id.button)) { frag = new Fragment(); FragmentManager fragmanag = getFragmentManager(); FragmentTransaction ft = fragmanag.beginTransaction(); ft.replace(R.id.fragment,frag); ft.commit(); } if (v == findViewById(R.id.button2)) { frag = new Frag(); FragmentManager fragmanag = getFragmentManager(); FragmentTransaction ft = fragmanag.beginTransaction(); ft.replace(R.id.fragment,frag); ft.commit(); } } }

Thats all! Thanks for reading!!

bottom of page