11

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

Amit Vaghela
  • 22,162
  • 20
  • 85
  • 137
Lucafraser
  • 829
  • 3
  • 12
  • 32
  • You put it in the wrong place, don't place it in your Manifest.xml but place it in your (YourActivity).xml where you create your activities layout – Strider Feb 12 '16 at 08:22
  • I can't tell whether it's just the way you formatted the code in your question, but any menu files need to have their own `xml` file in the `menus` sub-directory in the `res` folder - it shouldn't be part of the manifest. – PPartisan Feb 12 '16 at 08:24
  • Where do I need to put it, and put what? Do I need to create a new folder? See the image above – Lucafraser Feb 12 '16 at 08:28
  • 1
    Create a folder in your ***res*** (resources) folder, and name it ***menu***. Than inside that folder create an XML file and place your menu code there. Look at the answers below. – Strider Feb 12 '16 at 08:33

4 Answers4

22

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);
        }
    }
Amit Vaghela
  • 22,162
  • 20
  • 85
  • 137
  • Well the issue is solved but how do I add options? And shouldn't I see a menu logo in my application? – Lucafraser Feb 12 '16 at 08:41
  • your options are added now if you follow this code. try running code and inform again. – Amit Vaghela Feb 12 '16 at 08:43
  • I have the menu working. But I want it to be so that can still see the option menu, the three dots I mean! The options are overlapping it. And my buttons are not the color they are in Android Studio, how come? Added an image in my post. – Lucafraser Feb 12 '16 at 08:53
  • try adding --- android:orderInCategory="2" no of itemsand ---app:showAsAction="always" – Amit Vaghela Feb 12 '16 at 09:00
  • pop you are getting can not be changed it is system default @Lucafraser – Amit Vaghela Feb 12 '16 at 09:13
  • Okay and what is it that my background image and button colors aren't showing. Buttons are yellow; shows white. Background image is also not to be shown. – Lucafraser Feb 12 '16 at 09:14
  • it will be according to theme you have applied OR second option is to make it custom @Lucafraser – Amit Vaghela Feb 12 '16 at 09:15
  • Thank you for appriciation @Lucafraser – Amit Vaghela Feb 12 '16 at 09:50
  • can you help me with something else? Maybe chat? I have some annoying problems with my buttons and images not showing the colors and images I want them to show. – Lucafraser Feb 12 '16 at 09:51
  • yes, why not or ask another question so that other users will not be confused reagarding this question. – Amit Vaghela Feb 12 '16 at 09:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103271/discussion-between-amit-vaghela-and-lucafraser). – Amit Vaghela Feb 12 '16 at 09:55
3

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

IntelliJ Amiya
  • 73,189
  • 14
  • 161
  • 193
1

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;
}
Eric B.
  • 4,552
  • 2
  • 16
  • 33
  • When adding that 2nd part to my MainActivity.java I get errors such as "Cannot resolve symbol such as Inflater" – Lucafraser Feb 12 '16 at 08:34
1

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);
}
Zahan Safallwa
  • 3,829
  • 2
  • 22
  • 32