How to Apply onClickListener on Menu Item in Android?
Last Updated :
24 Apr, 2025
In the context of Android development, a "menu item" refers to an individual item in a menu. A menu is a visual element in an Android app that provides a set of options to the user, allowing them to perform various actions or navigate to different parts of the app.
A menu item is typically represented by an icon or text, or both, and when selected by the user, it triggers a specific action or opens a new screen. The menu items in an Android app can be displayed in a number of ways, including as a context menu, an options menu, or an action bar. In Android, menu items are defined in an XML file and can be inflated in the app's code to create the menu dynamically at runtime. The XML file defines the structure of the menu, including the text or icons that should be displayed for each menu item and the actions that should be taken when each item is selected. Now see how can we apply OnClick Listener to these menu items:
- Inflate the menu: In your Activity class, override the onCreateOptionsMenu method and use the MenuInflater class to inflate the menu resource file into the Menu object passed as a parameter.
- Identify the menu item: Use the findItem method to retrieve a reference to the desired menu item.
- Set an onClick listener: Use the setOnMenuItemClickListener method to set an onClick listener on the menu item.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Change the StatusBar Color
Navigate to app > res > values > themes > themes.xml and add the below code under the style section in the themes.xml file.
<item name="android:statusBarColor" tools:targetApi="l">#308d46</item>
Step 3: Creating a menu layout for the menu item
In this step, we are going to create the menu button that is displayed in the action bar. Navigate to app > res > create a new directory named menu then inside the menu folder create a new layout file named menu_items.xml. Below is the code for it.
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add_btn"
android:icon="@drawable/ic_btn_menu"
android:orderInCategory="100"
android:title="btn"
app:showAsAction="always" />
</menu>
- android:icon: This attribute sets the icon to be displayed for the menu item. The @drawable/ic_btn_menu reference points to a drawable resource in the app.
- android:orderInCategory: This attribute sets the order of the menu item relative to other items in the same category. Higher numbers mean the item will appear later in the menu.
- android:title: This attribute sets the text label for the menu item.
- app:showAsAction: This attribute determines how the menu item should be displayed on the screen. The value "always" means the item should always be visible on the action bar.
This XML code defines a menu with a single menu item with an icon, text label, and order, and that should always be visible on the action bar.
Step 4: Working with MainActivity
In this article, we are going to inflate the menu_items layout file and apply OnclickListener to it. Navigate to app > java > your package name > MainActivity. Below is the code for MainActivity.
Kotlin
package com.example.geeksforgeeks
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// Inflating the menu items from the menu_items.xml file
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_items, menu)
return super.onCreateOptionsMenu(menu)
}
// Handling the click events of the menu items
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Switching on the item id of the menu item
when (item.itemId) {
R.id.action_add_btn -> {
// Code to be executed when the add button is clicked
Toast.makeText(this, "Menu Item is Pressed", Toast.LENGTH_SHORT).show()
return true
}
}
return super.onOptionsItemSelected(item)
}
}
Java
package com.example.geeksforgeeks;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Inflating the menu items from the menu_items.xml file
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
return super.onCreateOptionsMenu(menu);
}
// Handling the click events of the menu items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Switching on the item id of the menu item
switch (item.getItemId()) {
case R.id.action_add_btn:
// Code to be executed when the add button is clicked
Toast.makeText(this, "Menu Item is Pressed", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
- onCreateOptionsMenu method: This method inflates the menu items from the menu_items.xml file using the menuInflater.inflate method and passing in the resource ID of the menu file and the menu object. The method returns the result of calling super.onCreateOptionsMenu method.
- onOptionsItemSelected method: This method handles the click events of the menu items. It switches on the item id of the menu item using a when statement and executes the code when the add button is clicked using the Toast class to display a message to the user. The method returns the result of calling super.onOptionsItemSelected method.
Output:
Similar Reads
How to Apply OnClickListener to RecyclerView Items in Android?
As we know applying OnClickListener to a Button is very simple but applying OnClickListener to a RecylerView item is different. In this article we first create a RecylerView when we click a particular item of the RecyclerView then a new activity will be shown and display the name and email of the pa
12 min read
How to Add OnClickListener to Marker on Google Maps in Android?
We have seen implementing Marker on Google Maps in Android. Now we will see adding OnClickListener for that marker on our Google Maps. In this article, we will take a look at adding OnClickListener to Google Maps marker in Android. What we are going to build in this article? We will be building a
3 min read
How to Apply One Listener to Multiple Buttons in Android?
In this article we are going to write a shortcode for applying click events over different buttons, rather than writing different methods for different buttons, we are going to build only a single method that is onClick() for all buttons present and by using the concept of switch case we can perform
3 min read
How to Add OnClickListener to Marker on Google Maps in Android using Jetpack Compose?
Many times in the android applications while using Google Maps, we have to add a click listener for the marker which we are going to display in our Google Maps. In this article, we will take a look at How to add an on-click listener for our marker on Google Maps in Android using Jetpack Compose. St
6 min read
How to Check Navigation Drawer Menu Items in Android?
Navigation Drawer or Slider Drawer is a UI component that is used in Mobile Applications and Websites to provide easy access to different features or parts of an app or website. Functioning of Navigation Drawer It is generally accessed by tapping on the hamburger menu icon (3 horizontal lines), pres
4 min read
How to Grouping Navigation Drawer Menu Items in Android?
Navigation Drawer or Slider Drawer is a UI pattern, used in Mobile Applications and Websites. It is used to provide easy access to different features or parts of an app or website.Functioning of Navigation DrawerIt is usually accessed by tapping on the hamburger menu icon (3 horizontal lines), prese
4 min read
How to Animate RecyclerView Items in Android?
RecyclerView Item animation is one of the modern features that we can add to our Android app, the basic working of this is when any user opens our app then the data items that are present in recycler view will animate and then it will show to the user.it is so easy to implement also it can enhance t
5 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement PDF Picker in Android?
In this article, we are going to see how we can implement a PDF picker in android studio and get the file information of the pdf like file name, size and path. In this application, we will create a launcher to launch the file picker and display the file information after a pdf is picked successfully
5 min read
How to Add ParticleSmasher in Android App?
ParticleSmasher is an Android library that allows us to easily particle effect to our views in our android app .we can use this feature in many apps such as the app in which we destroy a particular UI after completion of the task or when we delete a particular file. A sample GIF is given below to ge
9 min read