Gil Fitussi
Gil Fitussi

Reputation: 145

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?

Upvotes: 0

Views: 1013

Answers (1)

Joaquin Iurchuk
Joaquin Iurchuk

Reputation: 5637

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>

Upvotes: 1

Related Questions