Search code examples
androidactionbarsherlockandroid-actionbar

Fill Action bar SHerlock with items


I am implementing ActionBarSherlock. I have disabled icon and title. But still my items are not taking that space. I have used

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

Icon and title are disappeared. But that space is not filled up with my items. I have 5 items like

 <item android:id="@+id/Clear"
    android:title="Clear"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/load"
    android:title="Load"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/Undo"
    android:title="Undo"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/save"
    android:title="Save"
    android:showAsAction="ifRoom"></item>
<item android:id="@+id/settings"
    android:title="Settings"
    android:showAsAction="ifRoom"></item>

But only 3 of them are being shown. and action bar is not full. It's like

enter image description here

I want all the items to appear in the action bar. what should I do?

EDIT:

I am considering adding tabs to the bar. It gives Run time error. Here is my code for adding tabs (It should be pretty simple but I don't know what am i doing wrong here)

Tab tab = getSupportActionBar().newTab();
protected void onCreate(Bundle savedInstanceState) {

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    for (int i = 1; i <= 3; i++) {

        tab.setText("Tab " + i);

        getSupportActionBar().addTab(tab);
    }
    }

I think this code should be enough to add tabs to the bar. isn't it?


Solution

  • You can use onPrepareOptionsMen, if you use tabs or viewpager it can allow you to change the buttons according to the tab you are in (just had a switch case):

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    
        menu.clear();
        switch ((int) _viewPager.getCurrentItem()) {
            case 0:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
            case 1:
                getSupportMenuInflater().inflate(R.menu.your_menu, menu);
                break;
            case 2:
                getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }
    

    Or judte one simple menu

    @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();
            getSupportMenuInflater().inflate(R.menu.your_menu, menu);
            return true;
        }
    

    You only need to put your items in a menu xml file

    *To add tabs : *

    public class MainActivity extends SherlockFragmentActivity {
    private static final String     TAG = MainActivity.class.getSimpleName();
    private ViewPager               _viewPager;
    private ActionBar               _actionBar;
    private Tab                     _Tab;
    private TabsAdapter             _tabAdapter;
    private int                     _viewPagerOffScreenLimit        = 10;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            _viewPager = (ViewPager) findViewById(R.id.viewpager);
            _viewPager.setOffscreenPageLimit(_viewPagerOffScreenLimit);
            _tabAdapter = new TabsAdapter(this, _viewPager);
            _actionBar = getSupportActionBar();
            _actionBar.setTitle(getResources().getString(R.string.app_name));
            _actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            _actionBar.setHomeButtonEnabled(true);
    
            _tabAdapter.addTab(_actionBar.newTab().setCustomView(getTabIndicator(getString(R.string.tab), R.drawable.ic_launcher)), FragmentTab.class, null);
    }
    private View getTabIndicator(String text, int drawable) {
    
            View indicator = _inflater.inflate(R.layout.tabs, null);
            ((TextView) indicator.findViewById(R.id.tab_title)).setText(text);
            ((ImageView) indicator.findViewById(R.id.tab_icon)).setImageResource(drawable);
            return indicator;
        }
    
    public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
            private final Context               mContext;
            private final ActionBar             mActionBar;
            private final ViewPager             mViewPager;
            private final ArrayList<TabInfo>    mTabs   = new ArrayList<TabInfo>();
    
            static final class TabInfo {
                private final Class<?>  clss;
                private final Bundle    args;
                TabInfo(Class<?> _class, Bundle _args) {
                    clss = _class;
                    args = _args;
                }
            }
    
    
            public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
                super(activity.getSupportFragmentManager());
                mContext = activity;
                mActionBar = activity.getSupportActionBar();
                mViewPager = pager;
                mViewPager.setAdapter(this);
                mViewPager.setOnPageChangeListener(this);
            }
    
            public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
                TabInfo info = new TabInfo(clss, args);
                tab.setTag(info);
                tab.setTabListener(this);
                mTabs.add(info);
                mActionBar.addTab(tab);
                notifyDataSetChanged();
            }
    
            @Override
            public int getCount() {
                return mTabs.size();
            }
    
            @Override
            public Fragment getItem(int position) {
                TabInfo info = mTabs.get(position);
                return Fragment.instantiate(mContext, info.clss.getName(), info.args);
            }
    
            public void onPageSelected(int position) {
                mActionBar.setSelectedNavigationItem(position);
                ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
            }
    
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                Object tag = tab.getTag();
                for (int i = 0; i < mTabs.size(); i++) {
                    if (mTabs.get(i) == tag) {
                        mViewPager.setCurrentItem(i);
                    }
                }
            }
        }
    }