Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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): </p> <pre><code>@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); } </code></pre> <p>Or judte one simple menu </p> <pre><code>@Override public boolean onPrepareOptionsMenu(Menu menu) { menu.clear(); getSupportMenuInflater().inflate(R.menu.your_menu, menu); return true; } </code></pre> <p>You only need to put your items in a menu xml file </p> <p>*<em>To add tabs : *</em></p> <pre><code>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&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;(); static final class TabInfo { private final Class&lt;?&gt; clss; private final Bundle args; TabInfo(Class&lt;?&gt; _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&lt;?&gt; 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 &lt; mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i); } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload