Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Popup Menu


Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

The android.widget.PopupMenu is the direct subclass of java.lang.Object class.

Android Popup Menu Example

Let’s see how to create popup menu in android.

activity_main.xml

It contains only one button.

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“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.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     tools:context=“.MainActivity” >
  10.     <Button
  11.         android:id=“@+id/button1”
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_alignParentLeft=“true”
  15.         android:layout_alignParentTop=“true”
  16.         android:layout_marginLeft=“62dp”
  17.         android:layout_marginTop=“50dp”
  18.         android:text=“Show Popup” />
  19. </RelativeLayout>

popup_menu.xml

It contains three items as show below. It is created inside the res/menu directory.

File: poupup_menu.xml
  1. <menu xmlns:androclass=“http://schemas.android.com/apk/res/android” >
  2.     <item
  3.         android:id=“@+id/one”
  4.         android:title=“One”/>
  5.     <item
  6.         android:id=“@+id/two”
  7.         android:title=“Two”/>
  8.     <item
  9.         android:id=“@+id/three”
  10.         android:title=“Three”/>
  11. </menu>

Activity class

It displays the popup menu on button click.

File: MainActivity.java
  1. package com.Kreationnext.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 button1;
  13.          @Override
  14.          protected void onCreate(Bundle savedInstanceState) {
  15.           super.onCreate(savedInstanceState);
  16.           setContentView(R.layout.activity_main);
  17.           button1 = (Button) findViewById(R.id.button1);
  18.           button1.setOnClickListener(new OnClickListener() {
  19.            @Override
  20.            public void onClick(View v) {
  21.             //Creating the instance of PopupMenu
  22.             PopupMenu popup = new PopupMenu(MainActivity.this, button1);
  23.             //Inflating the Popup using xml file
  24.             popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
  25.             //registering popup with OnMenuItemClickListener
  26.             popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  27.              public boolean onMenuItemClick(MenuItem item) {
  28.               Toast.makeText(MainActivity.this,“You Clicked : “ + item.getTitle(),Toast.LENGTH_SHORT).show();
  29.               return true;
  30.              }
  31.             });
  32.             popup.show();//showing popup menu
  33.            }
  34.           });//closing the setOnClickListener method
  35.          }
  36.     }

Output:

popup1

popup2

popup3

Comments

comments