Blog Archives

PopupMenu Demo

The PopupMenu is anchored to a View. If a PopupMenu is shown in response to a click event to a button, the Popup will be displayed under the button if there is room.
The PopupMenu needs at least SDK 11. You need to open “AndroidManifest.xml” and change the minimum SDK version (“8″) to “11” since the ADT makes an app template with “8” for the minSdkVersion.

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

Just like any other Android API Guides, the section of the PopupMenu (http://developer.android.com/guide/topics/ui/menus.html#PopupMenu) is good for just lying-down-readers for fun but not good enough for someones who really need to use it.

Here I would like to provide a complete tutorial. But notice that this is not something that you can copy and paste it to build. You have to understand what you are doing.

First let us add a button to show a Popup menu. Please notice that the android:onClick attribute has the “showPopup” method name.

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".PopupMenuDemoActivity" >

    <Button
        android:id="@+id/buttonShowMePopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="showPopup"
        android:text="Show Me Popup" />

</RelativeLayout>

It is always good idea to use resources to maintain strings.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">PopupMenuDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="archive">Archive</string>
    <string name="delete">Delete</string>
    <string name="popup_menu_archive_selected">Archive is selected.</string>
    <string name="popup_menu_delete_selected">Delete is selected.</string>
</resources>

A new menu XML file must be added. Add a new Android XML with “Menu” Resource Type. Use “actions.xml” as its name. Please insert two items for this demo.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/archive" android:title="@string/archive"></item>
    <item android:id="@+id/delete" android:title="@string/delete"></item>
</menu>

Then we are pretty much ready to write code. For the sake of simplicity I decided to use the main activity (named PopupMenuDemoActivity) as a target of the “PopupMenu.OnMenuItemClickListener” interface. This means the activity should implement the interface. When the user selects an item, Android system will call the onMenuItemClick() callback.

package edu.kettering.popupmenudemo;

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

public class PopupMenuDemoActivity extends Activity 
		implements PopupMenu.OnMenuItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_popup_menu_demo);
	}

	public void showPopup(View v)
	{
		PopupMenu popupMenu = new PopupMenu(this, v);
		popupMenu.getMenuInflater().inflate(R.menu.actions, popupMenu.getMenu());
		popupMenu.setOnMenuItemClickListener(this);
		popupMenu.show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.popup_menu_demo, menu);
		return true;
	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		String msg;
		switch(item.getItemId()) {
		case R.id.archive:
			msg = getResources().getString(R.string.popup_menu_archive_selected);
			Toast.makeText(getApplicationContext(), 
                   msg, Toast.LENGTH_LONG).show(); 
			return true;
		case R.id.delete:
			msg = getResources().getString(R.string.popup_menu_delete_selected);
			Toast.makeText(getApplicationContext(), 
                    msg, Toast.LENGTH_LONG).show(); 
			return true;
		}
		return false;
	}

}

 

Follow

Get every new post delivered to your Inbox.

Join 147 other followers