1

I'm making a web browser, it has four tabs (tabs are done using tab host) and I need a separate options menu for each of the tabs. How would I go about doing so? Using boolean?

Thank you!

Edit (How the tabs are done)

TabHost th = (TabHost) findViewById (R.id.tabHost);

    th.setup();
    TabSpec specs = th.newTabSpec("Tab1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("Tab 1");
    th.setCurrentTab(1);
    th.addTab(specs);
Tssomas
  • 362
  • 1
  • 4
  • 16

1 Answers1

0

You could use a OnTabChangeListener to get a callback, when the user changed to another tab. Based on the tab ID, you can manipulate the options menu.

Call invalidateOptionsMenu() when you need to alter the menu. Then, onCreateOptionsMenu() will be called. You can then inflate another menu.xml. Alternatively, you could also wait for the subsequent call to onPrepareOptionsMenu() and toggle the visibility of single menu items via setVisible().

Example: (haven't tested it, but it should work more or less)

private static final String TAB_1 = "tab1";
private static final String TAB_2 = "tab2";

private TabHost mTabHost;

private final TabHost.OnTabChangeListener onTabChangedListener = new TabHost.OnTabChangeListener {
    public void onTabChanged(String tabId){
        invalidateOptionsMenu();
    }
};

public void onCreate(Bundle b){
    TabHost mTabHost = (TabHost) findViewById (R.id.tabHost);
    // ...
    mTabHost.setOnTabChangedListener(onTabChangedListener);
    mTabHost.setCurrentTabByTag(TAB_1);
}

public boolean onCreateOptionsMenu(Menu menu){
    final String currentTab = mTabHost.getCurrentTabTag();

    if(TAB_1.equals(currentTab)){
        getMenuInflater().inflate(R.menu.main_tab1.xml, menu);
    } else if(TAB_2.equals(currentTab)){
        getMenuInflater().inflate(R.menu.main_tab2.xml, menu);
    }
}
Brian
  • 2,130
  • 22
  • 29
  • Hello, thanks for helping! I'm a little confused, could you please give an example of the first method? – Tssomas Feb 23 '14 at 21:42
  • I added an example. Mind, that depending on your target API level, you need to include the support library. If so, the method calls would be `supportInvalidateOptionsMenu()` and `getSupportMenuInflater()`. – Brian Feb 24 '14 at 08:57