Fully Functioning ViewPager + TabListener for Android 4.0 (ICS) ActionBar Tabs

I spent about two weeks trying to develop a system of tabs for Android based on the ActionBar as can be seen in ICS’ People, GMail, Play Music, and Play Store. Google has said that this is the future of Android’s UI paradigm. Without further ado, here’s my implementation:

import java.util.ArrayList;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class Polling extends FragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private final static String TAG = "21st Polling:";
public void onCreate(Bundle savedInstanceState) {
mViewPager = new ViewPager(this);
final ActionBar bar = getActionBar();
mTabsAdapter = new TabsAdapter(this, mViewPager);
LoginFragment.class, null);
EconFragment.class, null);
ElectionsFragment.class, null);
PoliticsFragment.class, null);
ScienceFragment.class, null);
FinanceFragment.class, null);
ReligionFragment.class, null);
MilitaryFragment.class, null);
InternationalFragment.class, null);

And here’s the implementation of a subclass that both manages the ViewPager and sets up the TabListener:

public static class TabsAdapter extends FragmentPagerAdapter
	implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final ActionBar mActionBar;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;
			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
		public TabsAdapter(FragmentActivity activity, ViewPager pager) {
			mContext = activity;
			mActionBar = activity.getActionBar();
			mViewPager = pager;
		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
		public int getCount() {
			return mTabs.size();
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(), info.args);
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		public void onPageSelected(int position) {
		public void onPageScrollStateChanged(int state) {
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			Log.v(TAG, "clicked");
			Object tag = tab.getTag();
			for (int i=0; i<mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
		public void onTabReselected(Tab tab, FragmentTransaction ft) {}
		public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}
		public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {	
			Object tag = tab.getTag();
			for (int i=0; i<mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
		public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}


  1. I’ve just one question,

    If i have a button in one Fragment, how can i set an onClick evenement to go to another activity ?

  2. Im missing something here. How i connect this with xml ?
    and ABS isnt it in this code right?


    1. also, final ActionBar bar = getActionBar(); gives me an exception noSuchMethod

      1. Yep, you’ll see in the code that I just emailed to you the proper solutions 🙂

    2. Well, ABS is certainly in here, just not what’s published! I’d be glad to email you the same class with the ABS aspects included. I had forgotten that this post didn’t include the ABS changes.

      As to XML – what do you mean connect it with XML? Do you mean XML layouts? If so, all the stuff inside TabsAdapter helps set up the XML layouts. Basically, each one of those addTabs is a different class, which corresponding inflates a View. Since it is a SherlockFragment, the inflating is done inside onCreateView. Again, if you want me to email you a sample, I can do so.

  3. Can you send me the implementation with ABS? Can’t really understand how to deal with it!

    1. Sure David, but the problem is actually shipping out a copy with ABS preloaded into it. I have a lot of trouble sorting out the importing of ABS into a project in Eclipse as it stands, let alone exporting it. But I’ll work on it when I’m finished with the current thing I’m working on in my app. I’ll either respond again here or email you.


  4. Hey there,

    Great tutorial above! I have this working perfectly with 3/4 of my fragments, there is just one ListView that doesn’t seem to get populated once the ViewPager setup is added in. Any ideas on what would cause this as it is working fine before the change.Could I too get a copy of the project with ABS included? I might be able to spot some silly mistake I’m making!

    Thanks again for the post, it really helped!



    1. Sending along a copy with ABS already loaded in is something I’ve never quite figured out. I’ve just emailed you a working app that fetches data from an AsyncTask that then populates a ListView inside a dialog. It doesn’t directly address your issue with a viewpager (this app didn’t use one), but I think it might be helpful to have a look through it anyways. Look through it and comment here/email me if you have further questions with it.

      I don’t have a perfect example code set from this tutorial, to be honest. I can probably set one up this weekend with a ListView, though – as a number of people have had similar issues using a ListView with ViewPager.

      To be honest in my own code/app, I’ve not used the ListView method. Instead, I have a SherlockFragment that uses a generic XML layout with a TableLayout. Then programmatically, after getting data via AsyncTask, build a new tableRow from each set of data and add it to the TableLayout. So if you’re interested in that approach, I can probably get a direct sample of that code more easily, but I suppose I should probably sort out the ListView method as well.


  5. Hi David, I would like to request the xml layout (if you dont mind, I would like to request the source code also), could you please?
    actually I already searching View Pager on Android Pallete (Form Widgets, Layout, Composite, and until Others pallete) but I cannot find View Pager component. Could you help me how to add View Pager component to graphical layout / xml file?

    I still try to use ActionBarSherlock library and until now I still can not running ABS.

    Thanks in advance

Comments are closed.