1

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 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

..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
on each fragment you have to pass a xml file contains item for that action bar.
can u give me some exp @Murtaza Hussain
the step above are example in itself.
0

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

0

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”

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.