Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:26:44+00:00 2026-06-10T19:26:44+00:00

I have a late requirement change that requires tab navigation. I have already using

  • 0

I have a late requirement change that requires tab navigation. I have already using ActionBarSherlock in my application. Previously my UI consisted of Activities. However from what i read the TabBar requires Fragment Activities? How is there any way i can keep my existing code and implement the TabBar or would this require a complete re write of my application ? All of my activities extends SherlockActivity ?

Kind Regards,

import android.os.Bundle;
import android.support.v4.app.Fragment;
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 com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;




public class NewMainActivity extends SherlockFragmentActivity implements ActionBar.TabListener,
        ViewPager.OnPageChangeListener
{
    private ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textlayout);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
        mViewPager.setOnPageChangeListener(this);
        //mViewPager.setPageMarginDrawable(R.drawable.border);
        mViewPager.setPageMargin(16);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("First Tab").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Second Tab").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Third Tab").setTabListener(this));
    }

    @Override
    public void onPageScrollStateChanged(int arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        mViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft)
    {
        // TODO Auto-generated method stub

    }

    private class MainPagerAdapter extends FragmentPagerAdapter
    {
        public MainPagerAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            switch (position)
            {
            case 0:
                // return (mFragmentA = new FragmentA());

            case 1:
                // return (mFragmentB = new FragmentB());

            case 2:
                // return (mFragmentC = new FragmentC());

            }
            return null;
        }

        @Override
        public int getCount()
        {
            return 3;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:26:45+00:00Added an answer on June 10, 2026 at 7:26 pm

    Of course you won’t have to re-write your application. You just need some refractoring that’s all.

    First you should start by moving your code to SherlockFragment(s).

    So let’s say you have 3 Activities: ActivityA, ActivityB and ActivityC.

    1 – Create 3 SherlockFragments: FragmentA, FragmentB and FragmentC.

    2 – Move your code to the fragments and make the necessary modifications (e.g. Overrides, Context references, etc.).

    3 – Now to setup your main FragmentActivity with Tab navigation:

    public class MainActivity extends SherlockFragmentActivity implements
            ActionBar.TabListener, ViewPager.OnPageChangeListener {
    
        private FragmentA mFragmentA;
        private FragmentB mFragmenB;
        private FragmentC mFragmentC;
    
        private ViewPager mViewPager;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_layout);
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(new MainPagerAdapter(
                    getSupportFragmentManager()));
            mViewPager.setOnPageChangeListener(this);
            mViewPager.setPageMarginDrawable(R.drawable.border);
            mViewPager.setPageMargin(16);
            final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.addTab(actionBar.newTab().setText(R.string.tab_a_title)
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText(R.string.tab_b_title)
                    .setTabListener(this));
            actionBar.addTab(actionBar.newTab()
                    .setText(R.string.tab_c_title).setTabListener(this));
        }
    
        @Override
        public void onPageSelected(int position) {
            getSupportActionBar().setSelectedNavigationItem(position);
        }
    
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mViewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    
        private class MainPagerAdapter extends FragmentPagerAdapter {
            public MainPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                case 0:
                    return (mFragmentA = new FragmentA());
    
                case 1:
                    return (mFragmentB = new FragmentB());
    
                case 2:
                    return (mFragmentC = new FragmentC());
    
                }
                return null;
            }
    
    
            @Override
            public int getCount() {
                return 3;
            }
        }
    }
    

    main_layout.xml

    <LinearLayout 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"
        android:orientation="vertical" >
    
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it's late, I'm tired, and probably being quite dense.... I have written an application
I have been using themes for a while, and off late when I was
Off late I have been dumping relatively large tables using SSMS. The usual way
I have this problem with late binding: I am creating a grocery list application.
I have already tried PreRenderComplete and unload is too late
Of late I have been diving deep into web application security. While browsing I
I have been using and learning CSS3 a fair bit of late and enjoying
Say we have realized a value of TDD too late. Project is already matured,
Maybe i'm just up too late. I have an object that is a thin
I'm originally a C# developer (as a hobby), but as of late I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.