Display Popup Menu On Long Press of a View in Android
Last Updated :
29 Mar, 2022
Android Popup Menu displays a list of items in a vertical list which presents the view that invoked the menu and is useful to provide an overflow of actions that are related to specific content. In this tutorial, we will learn how to display a popup menu on the long-press of a view. We will learn it by making a project in android studio. Here we will be using Kotlin as the language for development. With the help of the menu, users can experience smooth and consistent experiences throughout the application. So to enhance the UI of the app, we use a popup menu on the long press of the view. It can be any view like ImageView, EditText, TextView, etc. For this project, we will be using an ImageView. When we long press on the Image, a popup menu will display a pop-up menu.
Step by Step Implementation
Step 1: Create a New Project.
To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.
Step 2: Add a vector asset in the drawable to use it as an image view
To add a vector asset go to:
app > res > drawable > new( right-click) > vector asset
Step 3: Working with the activity_main.xml file.
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="180dp"
android:layout_height="180dp"
android:src="@drawable/ic_image_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Working with the popup_menu.xml file.
Create a menu directory and then add a new resource file in the menu for the popup menu. To create a menu in Android Studio please refer to this article. Here we need to add the item that we need to show in the menu. We need to specify there's id and title. We can also add images along with the title. Here is the code for popup_menu.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/share"
android:title="SHARE"/>
<item
android:id="@+id/save"
android:title="SAVE"/>
<item
android:id="@+id/download"
android:title="DOWNLOAD"/>
</menu>
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code.
Kotlin
package com.ayush.popupmenu
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.Toast
import java.lang.Exception
class MainActivity : AppCompatActivity() {
lateinit var img: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
img = findViewById(R.id.img)
popupMenu()
}
private fun popupMenu() {
// creating a object of Popupmenu
val popupMenu = PopupMenu(this, img)
// we need to inflate the object
// with popup_menu.xml file
popupMenu.inflate(R.menu.popup_menu)
// adding click listener to image
popupMenu.setOnMenuItemClickListener {
when (it.itemId) {
R.id.share -> {
Toast.makeText(this, "Shared", Toast.LENGTH_SHORT).show()
true
}
R.id.save -> {
Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show()
true
}
R.id.download -> {
Toast.makeText(this, "downloaded", Toast.LENGTH_SHORT).show()
true
}
else -> {
true
}
}
}
// event on long press on image
img.setOnLongClickListener {
try {
val popup = PopupMenu::class.java.getDeclaredField("mPopup")
popup.isAccessible = true
val menu = popup.get(popupMenu)
menu.javaClass.getDeclaredMethod("setForceShowIcon", Boolean::class.java)
.invoke(menu,true)
}
catch (e: Exception)
{
Log.d("error", e.toString())
}
finally {
popupMenu.show()
}
true
}
}
}
So, our app is ready. And we can see the output.
Output:
Similar Reads
Count the Number of Taps (Multi-Tapping) on a View in Android In this article, the number of taps that user does in a  short span of time on a particular view is counted, It can be used for adding different response with different taps. For example one tap on the word will show the meaning of the word while double tap will show its synonyms in a dictionary app
3 min read
Popup Menu in Android With Example In Android development, Menus are an important part of the user interface, providing users with easy access to common functionalities and ensuring a smooth and consistent experience throughout the application. In Android, we have three types of Menus available to define a set of options and actions
4 min read
Double-Tap on a View in Android Detecting a double tap i.e. whenever the user double taps on any view how it is detected and according to the view a response can be added corresponding to it. Here an example is shown in which the double tap on the view is detected and corresponding to it a response is added in the form of toast. S
2 min read
How to Display a Yes/No DialogBox in Android? DialogBox is used in many android applications to display dialogs. There are different types of dialogs that are displayed within android applications such as AlertDialog, warning dialogs, and others. In this article, we will take a look at How to Display a Yes/No Dialog Box in Android. A sample vid
4 min read
Triple-Tap on a View Using onTouchListener Object in Android To detect a triple tap i.e. whenever the user triple taps on any view how it is detected and according to the view a response can be added corresponding to it. Here an example is shown in which the user triple taps on the view, it is detected and corresponding to the view response is added in the fo
2 min read
How to Create Option Menu in Android using Kotlin? In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read