Android Menu - Handling Option Menu Item Click
http://www.viralandroid.com/2015/09/handling-android-menu-item-click.html
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.
Related:
Buttons - Android Button Examples
Android Button onClick
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)
First, you have to add menu item to your menu_main.xml file, which seems like this.
res/menu/ menu_main.xml
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.
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.
Related:
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.