C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



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 {

    @Override
    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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="includehelp.articles.demo">

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

</FrameLayout>

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) {
        super(fm);
    }

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

    }

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

    @Override
    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"
    package="includehelp.articles">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Menu_Activity"
            android:label="@string/title_activity_menu_"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output:

Android Tabbed activity 2 - Output







You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions