Programmatically switch tabs in Android using ActionBarSherlock

I couldn't find any info about this but, how can i programmatically switch tabs in ActionBarSherlock?

Normally when i want to switch views i'd use something like:

Intent intentSecondView = new Intent(this, SecondView.class);

But obviously this doesn't work, because the views in the tabs are fragments.

So is there a way to switch between tabs by code when using ActionBarSherlock??

This is how i add an actionbar with tabs currently.

In my onCreate method i have:

    mViewPager = new ViewPager(this);

    ActionBar bar = getSupportActionBar();

    mTabsAdapter = new TabsAdapter(this, mViewPager);

            bar.newTab().setText("Fragment 1"),
            MyFragment1.class, null);
            bar.newTab().setText("Fragment 2"),
            MyFragment2.class, null);

I added nothing in my AndroidManifest file to create the tabs. It's all programmatically.


Try calling actionBar.setSelectedNavigationItem(x):

int position = 1;

In my app I have one tab fragment that has an album of pictures. When the user clicks on one of the pictures it causes that picture to be displayed in a ViewPager on the other tab fragment and automatically switches to that tab with setCurrentTabByTag().

public class EditAlbumTabs extends SherlockFragmentActivity {
   TabHost mTabHost;
   TabsAdapter mTabsAdapter;

   public void onPagerPositionSet(int pagerPosition, String[] imageUrls) {
        FragmentFlash fragmentFlash = (FragmentFlash)mTabsAdapter.getFragment("flash");
        if (fragmentFlash != null) {
           fragmentFlash.pagerPositionSet(pagerPosition, imageUrls);

protected void onCreate(Bundle savedInstanceState) {

    mTabHost = (TabHost)findViewById(;

    mViewPager = (ViewPager)findViewById(;
    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
            FragmentAlbumFlashum.class, null);
            FragmentFlash.class, null);

Need Your Help

Get Enum Instance from Class<? extends Enum> using String value?

java class reflection enums instantiation

I'm finding it difficult to put the exact question into words, so I'll just give an example.

What is indeterminate behavior in C++ ? How is it different from undefined behavior?

c++ c

What is the difference between an indeterminate behaviour and an undefined behaviour in C++? Is this classification valid for C codes also?