Introduction

This article explains PopUp menus in Android. Android Studio is used to create the sample.
A PopUp menu is a type of menu that contains a menu and displays that menu below the anchor text if space is available. If the space is not available then it displays it above the Anchor text. It appears untill when you do not click on the pop-up menu text.
For this you need to create an XML file inside "res/menu/popup" like this:
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  2. <item
  3. android:id="@+id/one"
  4. android:title="Android"/>
  5. <item
  6. android:id="@+id/two"
  7. android:title="BlackBerry"/>
  8. <item
  9. android:id="@+id/three"
  10. android:title="Apple"/>
  11. </menu>
Step 1
Create a project like this:
Clipboard04
Step 2
Create an XML file with this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:background="#fdacbd">
  7. <Button
  8. android:id="@+id/button"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:layout_marginLeft="110dp"
  14. android:layout_marginTop="80dp"
  15. android:text="Show Popup" />
  16. </RelativeLayout>
Step 3
Create another XML file with this:
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  2. <item
  3. android:id="@+id/one"
  4. android:title="Android"/>
  5. <item
  6. android:id="@+id/two"
  7. android:title="BlackBerry"/>
  8. <item
  9. android:id="@+id/three"
  10. android:title="Apple"/>
  11. </menu>
Step 4
Create a Java class file and with the following.
In this, you create the id of the button on which you pop up the menu. Now you will crate the instance of the PopUp menu, then you will populate.
  1. package com.popupmenu;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.PopupMenu;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12. Button button;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. button = (Button) findViewById(R.id.button);
  18. button.setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. //Creating the instance of PopupMenu
  22. PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
  23. //Inflating the Popup using xml file
  24. popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
  25. //registering popup with OnMenuItemClickListener
  26. popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  27. public boolean onMenuItemClick(MenuItem item) {
  28. Toast.makeText(MainActivity.this,"Button Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
  29. return true;
  30. }
  31. });
  32. popupMenu.show();//showing popup menu
  33. }
  34. });//closing the setOnClickListener method
  35. }
  36. }
Step 5
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.popupmenu"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.popupmenu.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 6
Popup menu
Clipboard06
Clipboard02