Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Handling Choice Selection in a Menu

Published? true
FormatLanguage: WikiFormat


After Creating a custom menu, implementing a reaction on clicking the menu item.


Overriding onOptionsItemSelected method.


In the Java Activity we need to override the onOptionsItemSelected. This method takes in a MenuItem and checks for its ID. Based on the ID of the item which is clicked, a switch-case can be used. Depending on the case selected, appropriate action can be taken. The custom menu would look something like this:

For a sample, the cases can just display Toasts.

Here's the source code for the sample:

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.icon1: 
            	Toast.makeText(this, "Icon 1 Beep Bop!", Toast.LENGTH_LONG).show();
            case R.id.icon2:
            	Toast.makeText(this, "Icon 2 Beep Bop!", Toast.LENGTH_LONG).show();
            case R.id.icon3:
            	Toast.makeText(this, "Icon 3 Beep Bop!", Toast.LENGTH_LONG).show();
            case R.id.icon4 :
            	Toast.makeText(this, "Icon 4 Beep Bop!", Toast.LENGTH_LONG).show();
        return true;


The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LZWM0ODRiNjAtNzJhOS00MGRjLTkwMjMtMjNlOTQwZDU0OGE2&hl=en_US&authkey=CJKD4IoH.