Implementing an optional menu in Android Studio

How to implement the options menu in an Android app? I tried the code from Android Developer , but I get errors. For example: A menu item should 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

+6
source share
4 answers

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); } 

Save the folder with the \ 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 the onOptionsItemSelected ie onClick event, which 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); } } 
+15
source

You should use onCreateOptionsMenu (Menu menu)

Initialize the contents of the menu of standard activity parameters. You should put menu items on the menu.

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

onCreateOptionsMenu (menu) , which must be overridden in the Activity class. This creates a menu and returns a Boolean value. bloat inflates the menu hierarchy from an 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 the demo. Android Option menu example.

+3
source

You need to create a menu folder in the res directory, and in the menu directory create a file called my_menu.xml. In this 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 the following:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); return true; } 
+1
source

You need to create menu.xml in the res-> menu menu, e.g. 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 the activity using the code below.

  @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); } 
+1
source

All Articles