Home » Android » android – Adding Menu in Action Bar

android – Adding Menu in Action Bar

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a Action Bar where i want to add one help button using Menu. I am using Android 3.0.
My Menu code is like below:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item  
    android:id="@+id/help_btn"
    android:icon="@drawable/help"
    android:title="Help"
    android:showAsAction="ifRoom|withText"
/>

Now how can i add this menu in the action bar??

How to&Answers:

The same way you create regular menus:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

http://developer.android.com/guide/topics/ui/menus.html#OptionsMenu

Answer:

I know this is a pretty old question, but I’ll throw an answer at it anyway. If you’re dealing with a Fragment, you’ll need to let the system know you’d like to contribute to the action bar or onCreateOptionsMenu will never be called. https://stackoverflow.com/a/10049807/725752

Answer:

Update;

You can inflate a menu like this @override

Put in res/menu/YOUR_MENU.xml

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.YOUR_MENU, menu);
    return true;
}