Loading...

Android Menu - Handling Option Menu Item Click

Menus are the user interface component and many popular applications also used option menu. In this example, you will learn to add menu item in android app and to handle onClick event. When a user clicks on a menu item, users see some message or open new activity. Here, I will show you technique to open new activity and show toast message when menu items get clicked.

Android Menu - Handling Option Menu Item ClickRelated:
Buttons - Android Button Examples
Android Button onClick

Create an Android Project


Create an android project with following information to implement menu in your app and to handle menu item click.
Application name: Menu Item Clickable
Company Domain: viralandroid.com
Package name: com.viralandroid.menuitemclickable
Minimum SDK: Android 2.2 (API 8 Froyo)

Add Menu Items


First, you have to add menu item to your menu_main.xml file, which seems like this.
res/menu/ menu_main.xml

Handling Menu Item Click Events


Now, what I’m going to do is, when you click menu icon, the new activity opens and when you click search and setting you will able see a toast message.

Modified code of MainActivity.java file seems like this.
src/MainActivity.java

Above Intent intent = new Intent(this,SecondActivity.class); allows you to open new activity. When you click new activity button and Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show();, displays a little message when you click menu items such as setting and search.


Create Second Activity


Your second activity java and XML file code seems like this.
src/SecondActivity.java

res/layout/second_activity.xml

And your AndroidManifest.xml file seems like this.

Finally, you have done all. Now, run your application which seems like this. If you have problem, please post your problem in the comment box.
Android Option Menu

Download Complete Example Project

Download complete menu item click project source code from GitHub.
Tutorial 456553695460679202
Home item