Home » Android

Android Tabbed Activity - 2

In this article, we will learn about how to make use of Fragment under Android Tabbed Activity?
Submitted by Manu Jemini, on January 12, 2018

Prerequisite:: Android Tabbed Activity - 1

You have learnt about how to create and set the activity in the last part? A Fragment is like a component which has a JAVA File and a Layout file just like an Activity. Creating a fragment is rather simple, the concept is very simple. It does not have anything very different than activity.

To create a fragment right-click on JAVA folder and select → new → fragment → blank fragment.

Tabbed activity 2 - 0

Now like activities you have to specify Fragment Name, also take care that you must check on create layout XML check-box, this will make you give a Fragment Layout Name. Also uncheck the Include fragment factory methods and include interface callbacks.

After this press FINISH.

Tabbed activity 2 - 1

You will have two files a JAVA and a XML.

JAVA File will look like this,

package includehelp.articles;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class demo extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { 
        return inflater.inflate(R.layout.fragment_demo, container, false);


Layout File:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <!-- TODO: Update blank fragment layout -->
        android:text="Fragment Text" />


Now you must add this Fragment in your Tabbed Activity. You can make anything of this fragment just like an activity.

The last section of JAVA FILE of menu activity should be like this,

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {

    public Fragment getItem(int position) {
        if(position == 0){
            return  new Demo();
            return PlaceholderFragment.newInstance(position + 1);


    public int getCount() {
        // Show 3 total pages.
        return 3;

    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Fragment";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        return null;

In here we have edited the functions and will got the desired result.

Menifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



Android Tabbed activity 2 - Output

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.