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 9153035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:11:37+00:00 2026-06-17T12:11:37+00:00

I am currently working on an android project and have created a little test

  • 0

I am currently working on an android project and have created a little test project to test the use of the Tab and changing tab by using a horizontal swipe gesture.

The code is pretty much working except for one problem. When I swipe the content to change the tab the activity for the second tab is shown but the actual tab isn’t selected. I.e. on android 4.2 when the app loads it starts up the main FragmentActivity and then displays the first fragment (fragment 1) which is inside tab 1. Tab 1 then has a blue line underneath to show that tab is selected.

If I then swipe the screen, the second activity is shown but the blue line stays under Tab 1 and doesn’t move across to tab 2.

However, if I select the actual tab instead of swiping then the blue line does move onto the second tab as expected.

I suspect I’ve missed something really simple from the tutorial I was looking at but I can’t find for the life of me what that might be.

Below is the code I am for the FragmentActivity. This is what is loaded when the app first starts.

public class TabsViewPagerFragmentActivity extends FragmentActivity 
    implements OnTabChangeListener, OnPageChangeListener{

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
    private PagerAdapter mPagerAdapter;

    private class TabInfo
    {
        private String tag;
        private Class<?> clss;
        private Bundle bundle;
        private Fragment fragment;
        public TabInfo(String tag, Class<?> clss, Bundle bundle) {
            this.tag = tag;
            this.clss = clss;
            this.bundle = bundle;
        }
    }

    class TabFactory implements TabContentFactory
    {
        private final Context mContext;

        public TabFactory (Context context)
        {
            mContext = context;
        }

        public View createTabContent(String tag)
        {
            View view = new View(mContext);
            view.setMinimumWidth(0);
            view.setMinimumHeight(0);
            return view;
        }   
    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null)
        {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
        this.initialiseViewPager();
    }

    protected void onSaveInstancestate(Bundle outState)
    {
        outState.putString("tab", mTabHost.getCurrentTabTag());
        super.onSaveInstanceState(outState);
    }

    private void initialiseViewPager()
    {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
        this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(this.mPagerAdapter);
        this.mViewPager.setOnPageChangeListener(this);
    }

    private void initialiseTabHost(Bundle bundle)
    {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
                this.mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1"),
                tabInfo = new TabInfo("Tab1", Tab1Fragment.class, bundle));
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
                this.mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2"),
                tabInfo = new TabInfo("Tab2", Tab2Fragment.class, bundle));

        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        mTabHost.setOnTabChangedListener(this);
    }

    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost,
            TabHost.TabSpec tabSpec, TabInfo tabInfo)
    {
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, 
            int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.mViewPager.setCurrentItem(position);
    }

    @Override
    public void onTabChanged(String tag) {
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }
}

Below is the code for the PageAdapter

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments)
    {
        super(fm);
        this.fragments = fragments;
    }

    public int getCount()
    {
        return this.fragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }
}

Thanks for any help you can provide.

  • 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-17T12:11:38+00:00Added an answer on June 17, 2026 at 12:11 pm

    Add mTabHost.setCurrentTab(position) to the onPageSelected() method in your FragmentActivity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on an Android project where I'd have to use maven.
I am currently working on android project where I want to have a text
I am currently working on an android project and I have an activity, lets
I am currently working on android project where I am using a custom list
I am currently working on an Android project. I am using a ListView and
I am currently working on an android project. I am trying to have an
I am currently working on an android project and making use of the SlidingDrawer
I am currently working on an Android project where we use string numbers with
I have a android project, I am working on and it, currently does not
I am currently working on android project and making use of fragments and ListViews/ListFragments.

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.