Android ActionbarSherlock Nested Fragment Tabs Tutorial - AndroidBegin
Android ActionbarSherlock Nested Fragment Tabs Tutorial - AndroidBegin
(http://www.androidbegin.com/)
Prepare your project by importing the ActionBarSherlock Library. Refer to
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
Implementing ActionBarSherlock in Android
(http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-in-android/)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.androidbegin.absnestedfragtab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
setContentView(R.layout.main_fragment);
// Locate android.R.id.tabhost in main_fragment.xml
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// Create the tabs in main_fragment.xml
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
// Create Parent Tab1
mTabHost.addTab(mTabHost.newTabSpec("parent1").setIndicator("Parent1"),
FragmentParentTab1.class, null);
// Create Parent Tab2
mTabHost.addTab(mTabHost.newTabSpec("parent2").setIndicator("Parent2"),
FragmentParentTab2.class, null);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
1
2
3
4
package com.androidbegin.absnestedfragtab;
import com.actionbarsherlock.app.SherlockFragment;
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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentParentTab1 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentparenttab1,
container, false);
return rootView;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.androidbegin.absnestedfragtab;
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentParentTab2 extends SherlockFragment {
FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getSherlockActivity());
mTabHost.setup(getSherlockActivity(), getChildFragmentManager(),
R.layout.fragmentparenttab2);
// Create Child Tab1
mTabHost.addTab(mTabHost.newTabSpec("child1").setIndicator("Child1"),
FragmentChildTab1.class, null);
// Create Child Tab2
mTabHost.addTab(mTabHost.newTabSpec("child2").setIndicator("Child2"),
FragmentChildTab2.class, null);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
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
37 }
1
2
3
4
5
6
7
8
9
10
11
12
13
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/FragmentParentTab1" />
</RelativeLayout>
1
2
3
4
5
6
<RelativeLayout 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" >
</RelativeLayout>
1
2
3
4
5
6
7
8
9
package com.androidbegin.absnestedfragtab;
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentChildTab1 extends SherlockFragment {
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.
10
11
12
13
14
15
16
17
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentchildtab1, container, false);
return rootView;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.androidbegin.absnestedfragtab;
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentChildTab2 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentchildtab2, container, false);
return rootView;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/FragmentChildTab1" />
</RelativeLayout>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/FragmentChildTab2" />
</RelativeLayout>
1
2
3
4
5
6
7
8
9
10
11
<resources>
<string name="app_name">ABS Nested Fragment Tabs Tutorial</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">ABS Nested Fragment Tabs Tutorial</string>
<string name="FragmentParentTab1">This is Fragment Parent Tab 1</string>
<string name="FragmentChildTab1">This is Fragment Child Tab 1</string>
<string name="FragmentChildTab2">This is Fragment Child Tab 2</string>
</resources>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbegin.absnestedfragtab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
3 Comments AndroidBegin Login 1
Sort by Newest Share Favorite
Output:
Source Code
ABSNestedFragTab (4.5 MiB, 503 downloads)
22
23
24
25
26
</activity>
</application>
</manifest>
Android Delete Multiple Selected Items
in ListView Tutorial
7 comments a year ago
sujith thycattussery How to change
imageview (flags) dynamically?
Android Search Filter XML Parse Images
and Texts Tutorial
20 comments a year ago
AndroidBegin Your file in inaccessible
directly. I'm getting this result "HTTP verb
used to access this page is not allowed."
which
Android Populating Spinner with JSON Android Parse.com Simple Login and
ALSO ON ANDROIDBEGIN
Join the discussion
Reply
Guest 7 months ago
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?
3
Reply
Ryan Kitlitz a month ago Guest
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.
Reply
tam nguyen 10 months ago
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.
1
WHAT'S THIS?
Share
Share
Share
Android Populating Spinner with JSON
Tutorial
18 comments 9 months ago
Stephen There will be a incomplete
coding in these tutorial.they didn't add
JSONFunction class.With the help of Http
request only we
Android Parse.com Simple Login and
Signup Tutorial
6 comments a year ago
Di Great Tutorial! thanks!One mistake
though:LoginSignupActivity.javaline 30 -