Tutorial details

Android beginner tutorial: Part 49 - Options Menu item listener | App Code for Sale | Preview

Android beginner tutorial: Part 49 - Options Menu item listener | Android Tutorial

How to listen to menu item selection in Options menu

Overview PAGE TOP

Today we'll learn how to listen to menu item selection in Options menu.

First let's remind ourselves how we added items to the menu. The onCreateOptionsMenu() function adds items using an external activity_main.xml file and using add() method of the menu object.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(Menu.NONE, IDM_NEW, Menu.NONE, "New");
menu.add(Menu.NONE, IDM_OPEN, Menu.NONE, "Open");
menu.add(Menu.NONE, IDM_SAVE, Menu.NONE, "Save");
menu.add(Menu.NONE, IDM_HELP, Menu.NONE, "Help");
menu.add(Menu.NONE, IDM_EXIT, Menu.NONE, "Exit");
return true;
}

Each item that was added using code has an identificator.

The activity_main.xml with menu items looks like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_settings"/>

</menu>

You can see that the items here don't have the same kind of identificators. Instead, the usual Android XML id syntax is used: "@+id/menu_settings".

We will be listening to the selection of an item in the menu and then determining which item has been selected using the id.

To add a listener, simply override a function called onOptionsItemSelected(). It receives a single parameter value of type MenuItem.

Inside the function, declare a "message" variable, then set its value depending on which item has been selected. In the end, display the message using a Toast.

To determine which item has been selected we can use item.getItemId() method and a switch..case statement. Check for each IDM_ identificator's value to handle all the programmatically added menu items. To handle the ones that were added using xml, use their identificator that was supplied in the xml, just make sure you format it properly (in our case - R.id.menu_settings is used):

@Override
public boolean onOptionsItemSelected(MenuItem item){
CharSequence message;
switch(item.getItemId()){
case IDM_NEW:
message="[New] selected";
break;
case IDM_OPEN:
message="[Open] selected";
break;
case IDM_SAVE:
message="[Save] selected";
break;
case IDM_HELP:
message="[Help] selected";
break;
case IDM_EXIT:
message="[Exit] selected";
break;
case R.id.menu_settings:
message="[Settings] selected";
break;
default:
return false;
}
Toast toast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
toast.show();
return true;
}

Full code here:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity{
public static final int IDM_NEW = 101;
public static final int IDM_OPEN = 102;
public static final int IDM_SAVE = 103;
public static final int IDM_HELP = 104;
public static final int IDM_EXIT = 105;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(Menu.NONE, IDM_NEW, Menu.NONE, "New");
menu.add(Menu.NONE, IDM_OPEN, Menu.NONE, "Open");
menu.add(Menu.NONE, IDM_SAVE, Menu.NONE, "Save");
menu.add(Menu.NONE, IDM_HELP, Menu.NONE, "Help");
menu.add(Menu.NONE, IDM_EXIT, Menu.NONE, "Exit");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
CharSequence message;
switch(item.getItemId()){
case IDM_NEW:
message="[New] selected";
break;
case IDM_OPEN:
message="[Open] selected";
break;
case IDM_SAVE:
message="[Save] selected";
break;
case IDM_HELP:
message="[Help] selected";
break;
case IDM_EXIT:
message="[Exit] selected";
break;
case R.id.menu_settings:
message="[Settings] selected";
break;
default:
return false;
}
Toast toast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
toast.show();
return true;
}

}

The results look like this (the menu and the Toast that appears when an item is selected):

lekok96.png

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-49.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.