Android ActionbarSherlock Nested Fragment Tabs Tutorial

In this tutorial, you will learn how to implement ActionbarSherlock Nested Fragment Tabs in your Android application. Nested fragment allows you to embed fragments inside fragments. Which means you can place dynamic and re-usable UI components in a UI component that is itself dynamic and re-usable. We will create parent tabs with fragments and inside the parent fragment will have child tabs with fragments using ActionbarSherlock Library. So lets begin…

Prepare your project by importing the ActionBarSherlock Library. Refer to Implementing ActionBarSherlock in Android tutorial.

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project ABSNestedFragTab.

Application Name : ABSNestedFragTab

Project Name : ABSNestedFragTab

Package Name : com.androidbegin.absnestedfragtab

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created two parent tabs with FragmentTabHost and each tab will open a different fragment.

Next, create an XML graphical layout for the MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file main_fragment.xml and paste the following code.

main_fragment.xml

Next, create the first fragment parent tab. Go to File > New > Class and name it FragmentParentTab1.java. Select your package named com.androidbegin.absnestedfragtab and click Finish.

Open your FragmentParentTab1.java and paste the following code.

FragmentParentTab1.java

Next, create the second fragment parent tab. Go to File > New > Class and name it FragmentParentTab2.java. Select your package named com.androidbegin.absnestedfragtab and click Finish.

Open your FragmentParentTab2.java and paste the following code.

FragmentParentTab2.java

In this fragment, we have created two child tabs with FragmentTabHost and each tab will open a different fragment.

Next, create an XML graphical layout for first fragment parent tab. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file fragmentparenttab1.xml and paste the following code.

fragmentparenttab1.xml

Next, create an XML graphical layout for second fragment parent tab. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file fragmentparenttab2.xml and paste the following code.

fragmentparenttab2.xml

Next, create the first fragment child tab. Go to File > New > Class and name it FragmentChildTab1.java. Select your package named com.androidbegin.absnestedfragtab and click Finish.

Open your FragmentChildTab1.java and paste the following code.

FragmentChildTab1.java

Next, create the second fragment child tab. Go to File > New > Class and name it FragmentChildTab2.java. Select your package named com.androidbegin.absnestedfragtab and click Finish.

Open your FragmentChildTab2.java and paste the following code.

FragmentChildTab2.java

Next, create an XML graphical layout for first fragment child tab. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file fragmentchildtab1.xml and paste the following code.

fragmentchildtab1.xml

Next, create an XML graphical layout for second fragment child tab. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file fragmentchildtab2.xml and paste the following code.

fragmentchildtab2.xml

Change the application name and fragment textview texts in strings.xml. Open your strings.xml and paste the following code. Go to res > values > strings.xml

strings.xml

In your AndroidManifest.xml, we need to change the theme style to “Theme.Sherlock” and set your preferable Android minimum SDK version. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

ABSNestedFragTab ScreenShots

Source Code 

ABSNestedFragTab (4.5 MiB, 537 downloads)
  • tam nguyen

    Hi , AndroidBegin
    i’m Tam, I’m from Viet Nam
    i’m asked you.

    i want 1 button in tab1, whe onclick button by send “hello” and open tab2.
    please help me.
    thank you.

  • Richard ‘Beanie’ Bean

    Hi!

    I’m unable to find the FragmentTabHost with:

    import android.support.v4.app.FragmentTabHost;

    I managed to foloow your previous tutorial on getting ActionBarSherlock tabs implemented, but the same project setup cannot find this library. Any thoughts?

    • Ryan Kitlitz

      Make sure the support library is included in your project.

      1. Right click your project, click properties.
      2. Click Java Build Path
      3. Click Libraries
      4. Click Add External Jars
      5. Locate android-support-v4.jar ( [sdk-install-path]/extras/android/support/v4/ )
      6. Click the Order and Export tab
      7. Make sure the checkbox next to android-support-v4.jar is selected.