Android Popup Menu With Examples
Android Popup Menu With Examples
In android, Popup Menu displays a list of items in a modal popup window that is anchored to
the view. The popup menu will appear below the view if there is a room or above the view in case
if there is no space and it will be closed automatically when we touch outside of the popup.
The android Popup Menu provides an overflow style menu for actions that are related to specific
content.
In android, the Popup Menu provides an overflow of actions that are related to specific content
and the actions in popup menu won’t affect the corresponding content. The popup menu won’t
support any item shortcuts and item icons.
In android, Popup menu is available with API level 11 (Android 3.0) and higher versions. If you are
using Android 3.0 +, the Popup Menu won’t support any item shortcuts and item icons in the
menu.
Once we are done with the creation of menu, we need to create a view element which anchored
the menu.
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup Menu"
android:onClick="showPopup" />
Now in our activity we need to implement showPopup method to show the popup menu.
1
Load Android Popup Menu from an Activity
To show the popup menu for the view, we need to instantiate Popup constructor and
use MenuInflater to load the defined menu resource using MenuInflater.inflate() like as shown
below.
To perform an action when the user selects a menu item, we need to implement
the PopupMenu.OnMenuItemClickListener interface and register it with our PopupMenu by
calling setOnMenuItemclickListener(). When the user selects an item, the system calls
the onMenuItemClick() callback in your interface.
Following is the example of handling a popup menu item click event using onMenuItemClick().
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
Note: If we are using Android 3.0 +, the Popup Menu won’t support any item shortcuts and item
icons in the menu.
2
Following is the example of implementing a Popup Menu in android application.
Create a new android application using android studio and give names as PopupMenuExample.
In case if you are not aware of creating an app in android studio check this article Android Hello
World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown
below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup Menu"
android:layout_marginTop="200dp"
android:layout_marginLeft="100dp"/>
</LinearLayout>
If we observe above code we created a one Button control in XML Layout file to show the popup
menu when we click on Button.
In android, to define popup menu, we need to create a new folder menu inside of our project
resource directory (res/menu/) and add a new XML (popup_menu.xml) file to build the menu.
Now open newly created xml (popup_menu.xml) file and write the code like as shown below.
popup_menu.xml
Once we are done with creation of menu, we need to load this menu XML resource from
our activity by instantiating a Popup constructor, for that open
3
main activity file MainActivity.java from \java\com.sarker.popupmenuexample path and write
the code like as shown below.
MainActivity.java
package com.sarker.popupmenuexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public
class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClic
kListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
popup.setOnMenuItemClickListener(MainActivity.this);
popup.inflate(R.menu.popup_menu);
popup.show();
}
});
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:
// do your code
return true;
case R.id.upload_item:
// do your code
return true;
case R.id.copy_item:
// do your code
return true;
case R.id.print_item:
// do your code
return true;
case R.id.share_item:
// do your code
return true;
case R.id.bookmark_item:
// do your code
return true;
default:
return false;
}
}
}
4
If we observe above code we are trying to show popup menu on Button click, loaded defined
menu resource using Popup.inflate() and implement popup menu items click event.
Generally, during the launch of our activity, onCreate() callback method will be called by android
framework to get the required layout for an activity.
This is how we can create Popup Menu in android applications to handle global actions.
Questions