Search code examples
androidandroid-layoutactionbarsherlockandroid-actionbar

Actionbar tabs overlapping on screen rotation


For some reason, when I change the orientation of the device, the actionbar is not properly rebuilt and the tabs (sometimes shown as a spinner) are overlapped with the other menu items.

I have 4 tabs (it works fine up to 3) (I use Actionbarsherlock, if relevant) On portrait, I use images instead of text for the tabs.

Here's a screenshot to explain:

enter image description here

And here's the code I use:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // Set up the action bar.
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setEnabled(false);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    }); 

    RebuildActionBar();
}

public void RebuildActionBar(){
    if(actionBar.getTabCount()>0) actionBar.removeAllTabs();
    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        if(screen_width>screen_height){
            actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
        } else {
            actionBar.addTab(actionBar.newTab()
                    .setIcon(mSectionsPagerAdapter.getPageIcon(i))
                    .setTabListener(this));
        }
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    RebuildActionBar();
}

In Manifest (just in case you ask, I have this working fine):

<activity
    ...
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
/>

1) I need, of course, to clean the ovelapping up.

And 2) If by any chance you can help me disable the spinner, even better. I want only tabs, even if I have to use images in both orientations.


Solution

  • ActionBarSherlock is a compatibility layer which exists inside the content view of an activity. This is a bit different than the native action bar which exists inside the window but outside of the normal content view.

    Because of this fact, it cannot properly recreate itself when you declare in the manifest that you handle orientation changes. If you prevent ABS from recreating the action bar views there are almost always noticible artifacts like what you picture.

    TL;DR: ActionBarSherlock does not work with configChanges="orientation".