Show popup menu in android kotlin code

Popup menu uses anchor to show. Create menu in res -> menu. Show popup menu on button click event.

Popup menu

Popup menu is similar to menu, when you clicked on more (three dots). But this menu can be used at any position in screen. You might have seen this type of menu in recyclerview item also.

Popup menu uses anchor to show. I have used it with button, so I can assign button as anchor of it.

Create Menu

Look for res -> menu folder, if not exists add new, right click on it and create new file named menu_popup like below :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app=""

    <item android:title="Android" android:icon="@drawable/ic_android_black_24dp"/>
    <item android:title="IOS" android:icon="@drawable/ic_android_black_24dp" />
    <item android:title="Windows"  android:icon="@drawable/ic_android_black_24dp"/>

Show Popup Menu

Get button in activity by findviewById or either with binding, and then in onClickListener of it use below code to show popup menu

val popupMenu = PopupMenu(this@PopupMenuExample, button)

// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(, popupMenu.getMenu())
popupMenu.setOnMenuItemClickListener { menuItem -> // Toast message on menu item clicked
        "You Clicked " + menuItem.getTitle(),
// Showing the popup menu
// Showing the popup menu

Above code create popup menu class, inflate menu layout in popup menu and show. Also specified menuitem clicks to show clicked item.

Margin for popup menu?

There might be some situation, we need to give some margin to our popup menu. To give some margin or spacing we need to create styles.xml file in res -> values (If not exists). Create style as below :

<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:dropDownHorizontalOffset">-8dp</item>
    <item name="android:dropDownVerticalOffset">16dp</item>

Now modify our popup menu creation like as below :

val popupMenu = PopupMenu(this@PopupMenuExample, button, Gravity.END,0,

Share this content:


More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message