Add Options Menu in Android

I have tried to add options menu to my application, but when adding a Create Options Menu to the code it is not used. It looks like this:

IDE snapshot showing unused handler

I used in this page on Fragment Activity and implements Tab Listener.

What is the right way to add the Options Menu?

Answer

In the fragment activity where you want to have the menu (Make sure you are importing android.support.v4.app.FragmentActivity):

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

In res/menu folder you should have this file: main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/itemId"    
        android:icon="@drawable/ic_launcher"
        android:showAsAction="ifRoom|withText"
        android:title="@string/yourString"/>
</menu>

Leave a Reply

Your email address will not be published. Required fields are marked *