What is popup menu

Android popup menu is a model window anchored to a view. The popup menu display below the anchored view.it is closed when you click out side of popup menu./p>

In my program i am going to create a simple popup menu when you press onto the button it will display the popup menu.

Popup Menu:.

In this section i am going to show the popup menu with practical example.

First open the android studio and create a project and give the name of your project like PopupMenuDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop one button from pallet on to the MainActvity.

Right click on res folder and a pop up menu will display click on new and then select the Android resource file a dialog box will display where you need to write your file name(main_menu) and Resource type as Menu.Figure display all the info do according the figure.

Smiley face

Open the main_menu.xml file and code look like this.

<menu xmlns:android="http://schemas.android.com/apk/res/android"





Open the MainActivity.java file and code look like this.

package com.example.niitpc.optionmenudemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button popupMenuButton;
protected void onCreate(Bundle savedInstanceState) {
  new View.OnClickListener() {
     public void onClick(View v) {
        PopupMenu popupMenu=new PopupMenu(getApplicationContext(),v);
          new PopupMenu.OnMenuItemClickListener() {
          public boolean onMenuItemClick(MenuItem item) {

          Toast.makeText(getApplicationContext(), item.toString(), Toast.LENGTH_LONG).show();
          return false;





Button PopupMenuButton:Here i am going to declare the popupMenuButton object of the Button.

popupMenuButton=(Button)findViewById(R.id.button):This statement initialized the popupMenuButton object .

popupMenuButton.setOnClickListener:This statement create the listener for button.

PopupMenu popupMenu=new PopupMenu(getApplicationContext(),v):This statement create the popupMenu object of PopMenu.

popupMenu.inflate(R.menu.main_menu):This statement inflate the menu.

popupMenu.show():This statement show the popup menu.

popupMenu.setOnMenuItemClickListener:This statement create the listener for popupMenu.

onMenuItemClick(MenuItem item):This method is activate when the user click on item of popup menu.

Toast.makeText(getApplicationContext(), item.toString(), Toast.LENGTH_LONG).show():This statement display the selected item of the popup menu in Toast.

Popup Menu

What is PopupMenu for video Click here

Smiley face                     Smiley face