PopUp Menu in Android

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.   
  9.     <item  
  10.         android:id="@+id/three"  
  11.         android:title="Apple"/>   
  12. </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.    
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_marginLeft="110dp"  
  15.         android:layout_marginTop="80dp"  
  16.         android:text="Show Popup" />  
  17.    
  18. </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.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.PopupMenu;  
  11. import android.widget.Toast;  
  12. public class MainActivity extends Activity {  
  13.     Button button;  
  14.    
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         button = (Button) findViewById(R.id.button);  
  20.         button.setOnClickListener(new OnClickListener() {  
  21.    
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 //Creating the instance of PopupMenu  
  25.                 PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);  
  26.                 //Inflating the Popup using xml file  
  27.                 popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());  
  28.                 //registering popup with OnMenuItemClickListener  
  29.                 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
  30.                     public boolean onMenuItemClick(MenuItem item) {  
  31.                         Toast.makeText(MainActivity.this,"Button Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
  32.                         return true;  
  33.                     }  
  34.                 });  
  35.                 popupMenu.show();//showing popup menu  
  36.             }  
  37.         });//closing the setOnClickListener method  
  38.     }  
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.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.popupmenu.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Step 6
 
Popup menu
 
Clipboard06
 
Clipboard02


Similar Articles