Implementing an option menu in Android Studio

60k views Asked by At

How do I implement an option menu in my android application? I tried code from Android Developer but I get errors. Such as these: Element menu must be declared. Here is my code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lucavanraalte.test" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <activity android:name=".MainActivity" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

enter image description here

5

There are 5 answers

11
Amit Vaghela On BEST ANSWER

In your java code, add this onCreateOptionsMenu to show optionMenu,

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); //your file name
        return super.onCreateOptionsMenu(menu);
    }

Keep your under res\menu\option_menu folder,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>

Now, if you want to set onOptionsItemSelected i.e onClick event for that ou can use,

@Override
    public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.new_game:
                //your code
                // EX : call intent if you want to swich to other activity 
                return true;
            case R.id.help:
                //your code
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
1
Eric B. On

You need to create a menu folder in the res directory and in the menu directory create file named my_menu.xml. In that file write these lines:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>

Then in your Activity, do this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}
0
Zahan Safallwa On

You need to create a menu.xml in directory res->menu like menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>

Then you need to create your menu from activity with below code

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.help) {

       //do something
       return true;
    }
    if (id == R.id.new_game) {

       //do something
       return true;
    }
    return super.onOptionsItemSelected(item);
}
0
IntelliJ Amiya On

You should use onCreateOptionsMenu (Menu menu)

Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu.

This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).

onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); // set your file name
        return super.onCreateOptionsMenu(menu);
    }

Your option_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item_First" 
          android:title="@string/item_First"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/save_menu" 
          android:title="@string/save"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/item_Second"
          android:title="@string/item_First"
          android:showAsAction="ifRoom"/>

</menu> 

Please check demo Android Option Menu Example

1
Nadeem Bhat On
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Home");
    menu.add("Profile");
    menu.add("Settings");
    menu.add("Privacy Policy");
    menu.add("Terms of use");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getTitle()=="Home"){
        Toast.makeText(this, "Home option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Profile"){
        Toast.makeText(this, "Profile option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Settings"){
        Toast.makeText(this, "Settings option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Privacy Policy"){
        Toast.makeText(this, "Privacy Policy option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Terms of use"){
        Toast.makeText(this, "Terms of use option selected", Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}