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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:09:46+00:00 2026-05-28T05:09:46+00:00

EDIT2 : I’m going to be more clear so everybody understands my question. I

  • 0

EDIT2: I’m going to be more clear so everybody understands my question. I hope more people can help me out!

What I made is an app wich contains fragments and tabs to switch between the fragments. I also made it possible to scroll/slide between the fragments using ViewPager.

The only problem now is that the tabs are not scrolling along with the fragments. So when I slide to the last fragment, I can’t see the selected tab because it’s out of viewingrange.

How can I make it possible to Scroll to the Tabs when I slide between the fragments?

I think I have to do something with this part of the code:

public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    this.mTabHost.setCurrentTab(position);
}

But I don’t have any clue and everything I tried doesn’t work. To make is easier here are two important parts of the code. I hope there’s someone that can help me!!

My main code:

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

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

    private class TabInfo {
         private String tag;
         TabInfo(String tag, Class<?> clazz, Bundle args) {
             this.tag = tag;
         }
    }

    class TabFactory implements TabContentFactory {
        private final Context mContext;

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

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

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

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

    private void intialiseViewPager() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, TabFragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, TabFragment2.class.getName()));
        fragments.add(Fragment.instantiate(this, TabFragment3.class.getName()));
        fragments.add(Fragment.instantiate(this, TabFragment4.class.getName()));
        fragments.add(Fragment.instantiate(this, TabFragment5.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 args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Home"), ( tabInfo = new TabInfo("Tab1", TabFragment1.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Messages"), ( tabInfo = new TabInfo("Tab2", TabFragment2.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Search"), ( tabInfo = new TabInfo("Tab3", TabFragment3.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Profile"), ( tabInfo = new TabInfo("Tab4", TabFragment4.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab5").setIndicator("Settings"), ( tabInfo = new TabInfo("Tab5", TabFragment5.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        mTabHost.setOnTabChangedListener(this);
    }

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

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

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // TODO Auto-generated method stub
    }

    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        this.mTabHost.setCurrentTab(position);
    }

    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub
    }
}

WHAT I DID SO FAR CAUSES THE TABS TO DISAPEAR AND REAPEAR WHEN I CLICK THEM:

public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    this.mTabHost.setCurrentTab(position);

    this.hScrollView = (HorizontalScrollView)super.findViewById(R.id.scroller);
    this.hScrollView.scrollTo(((int)(mTabHost.getWidth() * (position / ((double)(mTabHost.getChildCount() - 1))))), 0);
}
  • 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-05-28T05:09:47+00:00Added an answer on May 28, 2026 at 5:09 am

    I don’t know the answer, but this guy implemented an open source library for what you’re doing. Hope that helps!

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

Sidebar

Related Questions

EDIT2: OK this is weird. (I can't answer my own question, so I just
Edit2: I just want to make sure my question is clear: Why, on each
edit2: Well I found out what was wrong. answered my own question. * Face-palm*
EDIT2: Thank you all for your help! EDIT: on adding @staticmethod, it works. However
EDIT1 : I tried doing sqlflush to reset everything, but that didn't help. EDIT2
Edit2: Can I do polymorphism with Union? It seems to me that I can
Possible Duplicate: Checking available stack size in C EDIT2: My question is Duplicate of
EDIT2 Looks like I can't read properly, disregard this post, I will try to
I got two TEdit controls. When I tab out of edit1, edit2 receives the
Edit2: After finally being able to profile the two against each other, it appears

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.