circler
circler

Reputation: 369

Actionbar with menu for each tab

Hello I am using ActionBar tabs to create a tabbed application similar to an iPhone one. The iPhone app has the tabs on the bottom and the action bar menu items on the top. Like this:

iphone app

You see how the "Today" and "Timeline" buttons are on the top of the action bar if we may call it in the iPhone app.

Now what I tried to do showed up like this:

android app

You can see the tabs are on the top and also the only menu in the action bar shown is the menu of the main activity that sets up the ActionBar and Adds the Fragments as Tabs.

How can I show a different menu in each tab and if possible make the Tabs appear top most.

Thanks in advance.

Upvotes: 0

Views: 147

Answers (1)

Kris
Kris

Reputation: 133

Make your activity implement OnTabChangeListener.

Then override the below method as follows on tab change

@Override
 public void onTabChanged(String tabId) {
  // TODO Auto-generated method stub

  invalidateOptionsMenu();
 }

Then you have to create a new menu based on the selected tab as shown below

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  final String currentTab = mTabHost.getCurrentTabTag();
  if (currentTab.equals("TimeLine")) {
   getMenuInflater().inflate(R.menu.timeline, menu);
  } else if (currentTab.equals("Calendar")) {
   getMenuInflater().inflate(R.menu.calendar, menu);
  }
  return true;
 }

Handle your menu item selections based on the tab id in the below method

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

 }

Upvotes: 1

Related Questions