Tutorial details

Android Action Bar with Tab | App Code for Sale | Preview

Android Action Bar with Tab | Android Tutorial

How to create tab in action bar with fragments

Overview PAGE TOP

Action bar was introduced from API level 11. In this post I will explain how to create tab in action bar with fragments. The final result is shown below where user can move between tabs.


Creating the Action bar and the tabs

The first step is getting the action bar reference and add the tab to it:

01  @Override
02  protected void onCreate(Bundle savedInstanceState) {
03      super.onCreate(savedInstanceState);  
04      ActionBar bar = getActionBar();
05      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
07      for (int i=1; i <= 3; i++) {
08          Tab tab = bar.newTab();
09          tab.setText("Tab " + i);
10          tab.setTabListener(this);
11          bar.addTab(tab);
13      }
14  }

Notice that first of all we don’t use any “main” layout. Second at line 3 we get the reference to the action bar simply using the method getActionBar. At line 4 we set the navigation mode in our case NAVIGATIONMODETABS. There three different navigation type supported:

  • Navigation mode list
  • Navigation mode standard
  • Navigation mode tabs

After these steps we have to create our tabs and add it to the action bar. In our example we create three different tabs. What we want is when user touches one of the tab the UI content changes. To achieve it we need two things:

  • Fragment that fills the UI when user changes the tab according to the tab selected
  • A listener that gets notification when user interacts with the tabs

Tab Fragment

In our example fragment will be very simple, it just shows a text in the middle of the screen. The layout looks like:

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="match_parent"
04      android:layout_height="match_parent"
05      android:orientation="vertical" >
07      <TextView android:id="@+id/msg"
08                android:layout_width="wrap_content"
09                android:layout_height="wrap_content"
10                android:layout_centerInParent="true"
11              />
13  </RelativeLayout>

While the fragment source code is very simple:

01  public void onCreate(Bundle savedInstanceState) {       
02      super.onCreate(savedInstanceState);
03      Bundle data = getArguments();
04      index = data.getInt("idx");
05  }
07  @Override
08  public View onCreateView(LayoutInflater inflater, ViewGroup container,
09          Bundle savedInstanceState) {
11      View v = inflater.inflate(R.layout.fragment, null);
12      TextView tv = (TextView) v.findViewById(R.id.msg);
13      tv.setText("Fragment " + (index + 1));
15      return v;
17  }

Just notice that we pass the fragment index using arguments bundle (line 2,3).Now we have our fragment we have simply to implement the listener.

Tab listener

We can make our Activity implements the listener so that when user selects a tab we show the relative fragment.

01  public class MainActivity extends Activity implements TabListener {
02  ...
04      @Override
05      public void onTabSelected(Tab tab, FragmentTransaction ft) {
06          Fragment f = null;
07          TabFragment tf = null;
09          if (fragList.size() > tab.getPosition())
10                  fragList.get(tab.getPosition());
12          if (f == null) {
13              tf = new TabFragment();
14              Bundle data = new Bundle();
15              data.putInt("idx",  tab.getPosition());
16              tf.setArguments(data);
17              fragList.add(tf);
18          }
19          else
20              tf = (TabFragment) f;
22          ft.replace(android.R.id.content, tf);
24      }
26      @Override
27      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
28          if (fragList.size() > tab.getPosition()) {
29              ft.remove(fragList.get(tab.getPosition()));
30          }
32      }
33  }

TabListener has several methods we have to override. The most important is onTabSelected that is called when user selects a tab. In this method first we check if we have our fragment in the fragment list (line 9-10), if so we reuse it and show the fragment (see line 20). If not, we create our fragment line (12-18) and add it to our fragment list. At the end (line 22), we simply replace the UI content (android.R.id.content) with the right fragment. The result is shown below:


Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.