I developed a navigation drawer which has number of fragment,s and action bar. I want to know items on action bar change when i change the fragment .
3 Answers
As per the guide you can do that
First: Create a menu file for fragment
Second:
onCreate() method of Fragment set setHasOptionsMenu(true);
Third:
Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.
Fourth:
Override onOptionItemSelected in your fragment for item handlers.
4 Comments
Deep Singh
..thnks for ans..i want to know one more thing .in my actionbar there is already a no of items ..how i can remove them when call to a specific fragment and want to show only that fragment ,s Item that i have inflate through menu of that fragment
Murtaza Khursheed Hussain
on each fragment you have to pass a xml file contains item for that action bar.
Deep Singh
can u give me some exp @Murtaza Hussain
Murtaza Khursheed Hussain
the step above are example in itself.
you can access your menu items once you have set up "on create Options Menu as follows
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_collections_new, menu)
val menuItem2:MenuItem=menu.findItem(R.id.men_item2)
menuItem2.title="new title2"
super.onCreateOptionsMenu(menu, inflater)
}/
Cheers...
Comments
Can you please try this
// declare a menu object at the top of the activity
Menu AppMenu;
// instantiate the AppMenu instance in the activity’s
onCreateOptionsMenu(Menu menu)
// with code similar to:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.myMenuLayout, menu);
AppMenu = menu;
return true;
}
// declare a menu object at the top of the activity
Menu AppMenu;
// instantiate the AppMenu instance in the activity’s
onCreateOptionsMenu(Menu menu)
// with code similar to:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.myMenuLayout, menu);
AppMenu = menu;
return true;
}
// in the button click handler or menu item code, switch the title as
needed
AppMenu.findItem(R.id.menuitem_expense_accept).setTitle("Expenses");// or
“Accept”
// in the button click handler or menu item code, switch the title as
needed
AppMenu.findItem(R.id.menuitem_expense_accept).setTitle("Expenses");// or
“Accept”