Tutorials Android Android Popup Menu Example

Android Popup Menu Example

In example explains how to create Popup menu in android. Popup menu is used to display the global actions. Popup menu is an overflow menu like Spinner actions. PopupMenu is available from API level 11 (Android 3.0).

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fcfcfc" >

    <Button
        android:id="@+id/btn_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#176CEC"
        android:text="SHOW POPUP"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

</RelativeLayout>

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item_movies"
        android:showAsAction="ifRoom|withText"
        android:title="Movies"
        android:visible="true"/>
    <item
        android:id="@+id/item_music"
        android:showAsAction="ifRoom|withText"
        android:title="Music"
        android:visible="true"/>
    <item
        android:id="@+id/item_comedy"
        android:showAsAction="ifRoom|withText"
        android:title="Comedy"
        android:visible="true"/>

</menu>

PopMenuActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

public class PopMenuActivity extends Activity implements OnMenuItemClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn_click).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				PopupMenu popupMenu = new PopupMenu(PopMenuActivity.this, view);
				popupMenu.setOnMenuItemClickListener(PopMenuActivity.this);
				popupMenu.inflate(R.menu.popup_menu);
				popupMenu.show();
			}
		});
	}

	public boolean onMenuItemClick(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item_comedy:
			Toast.makeText(this, "Comedy Clicked", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_movies:
			Toast.makeText(this, "Movies Clicked", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.item_music:
			Toast.makeText(this, "Music Clicked", Toast.LENGTH_SHORT).show();
			return true;		
		}
	}
}

Output
Popup Example in Android

Total
0
Shares
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Bishal
Bishal
7 years ago

how can we set a custom layout for the pop up menu. I mean your Button’s width and pop up menu’s width must be same so in that case

jyotsana
jyotsana
7 years ago

vry nice article!
this may also help u :
Show Simple Pop-up Menu in Android

ravishankar yadav
ravishankar yadav
6 years ago

i want popmenu show with icon and also source code to change the icon of menu item at runtime…becoz i use menu as off on service so on off change icon and On change another icon

John Clinton Mwangi
John Clinton Mwangi
6 years ago

How can I make this work for lower API. My app is for android 2.3 API 9. Please help. Thanks

Previous Post
Android Tutorials

Share File to Dropbox in Android Example

Next Post
Facebook SDK

Using Facebook SDK in Android Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
4
0
Would love your thoughts, please comment.x
()
x