Skip to content Skip to sidebar Skip to footer

Implementing An Option Menu In Android Studio

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 <

Solution 1:

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

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        MenuInflaterinflater= getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); //your file namereturnsuper.onCreateOptionsMenu(menu);
    }

Keep your under res\menu\option_menu folder,

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/new_game"android:icon="@drawable/ic_new_game"android:title="@string/new_game"android:showAsAction="ifRoom"/><itemandroid: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,

@OverridepublicbooleanonOptionsItemSelected(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 returntrue;
            case R.id.help:
                //your codereturntrue;
            default:
                returnsuper.onOptionsItemSelected(item);
        }
    }

Solution 2:

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.

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        MenuInflaterinflater= getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); // set your file namereturnsuper.onCreateOptionsMenu(menu);
    }

Your option_menu.xml

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

Please check demo Android Option Menu Example

Solution 3:

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"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/new_game"android:icon="@drawable/ic_new_game"android:title="@string/new_game"android:showAsAction="ifRoom"/><itemandroid:id="@+id/help"android:icon="@drawable/ic_help"android:title="@string/help" /></menu>

Then in your Activity, do this:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    MenuInflaterinflater= getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    returntrue;
}

Solution 4:

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

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    returntrue;
}

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {

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

       //do somethingreturntrue;
    }
    if (id == R.id.new_game) {

       //do somethingreturntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

Post a Comment for "Implementing An Option Menu In Android Studio"