Prerequisite

If you not understand this lecture Popup Menu in android studio.You should also read my previous lectures Option Menu in android studio and Contextual Action mode in android studio . After reading these lecture you will also able to create Popup Menu of your app.

Overview

A popup menu is a graphical user interface display area usually a small window that suddenly appears A special kind of menu option .which appears just below the item you selected. A popup menu offers a limited set of choices that are available in the current state of application.

Introduction

In this lecture well we describe how can create Popup menu android studio.And also describe what is Popup menu.why we can use it.After Reading this lecture you will understand popup menu in android And i hope you will create popup menu of your application.

 Option menu and Popup menu

Android option menu is used to show multiple options for the application. when the button is pressed. Menu are show on that activity.Menu consists items and sub item.You can add, insert, and remove option menu. Popup menu A special kind of menu option .which appears just below the item you selected. A popup menu offers a limited set of choices that are available in the current state of application.

Description of Popup menu in android

Popup menus in android are useful for displaying  options associated with a specific action.In this lecturer will describer how can create a Popup menu in android studio.Popup menu in android is available in android 3.0 if you will create popup menu to your activity then you set minimum  API 11 and above .Popup menu is similar to spinner view in android .So we create a new project in android studio.Every project contain two file first is java class file and second is layout XML file. First of all we will open a layout.XML file a add button and change the text of button into login and also define on-click method in a button. Suppose the application that you will create have two type of user.So we attached a popup menu into the button.when user click on button its show popup menus one for admin other one for user for more detail about Popup menu in android please click on popup menu in android.

First of all you will create a new menu . how can create menu layout detail is provides step by step in a below pic.

Step 1=Add menu<-Right click the menu folder-><- select new option->and click on-<Menu resource folder->

Alt Tag android add  new menu.xml image

Step2=Step1<-Writename->ok

Alt tag android menu assign name menu

For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code .you should define a menu and all its items in an XML.

Code that are write in menu.xml file

After this step you will open a Layout.XML file.And add button and set height and width.

Code that are write in Layout. XML file

Code that are write MainActivity. java class file.

Handling Click Events

For handling click events in popup menu in android. we create a new java class file how can create java class file detail is provide in a below pic

Step2=Create new class  ->Right click-> the ->package name ->select ->New option-> and click-> java class that will show in below.  

Alt Tag Android make new class

Step3=Step2->WriteName->Ok

Alt Tag option menu in android

For handling the click events onMenuItemClick method is used.To perform an action when the user selects a menu item, you must implement onMenuItemClick method. we will used two alternative. if user select first option then first condition will execute if user select second option then second condition will execute and used Toast.makeText to show message which item user will select.

Code that are write PopupMenuEventHandle. java class file.

Run the Application 

Run the application and click on a login button then Popup menu are show.

Alt Tag android popup menu in android

when user click on a first option(Admin) then a message  show in a below pic similarly if a user click on second option second message will be show.

Alt Tag android option Menu output 2

Menu with Radio Button

We will describe how can create a select-able item. For select-able item we will add checkbox and radio button in menu .You can use Radio button and checkbox using checkableBehavior option for more detail please click on Menu with radio button. In this lecture will describe how can add a Radio button in menu.So we create a new project in android studio.Every project contain two file first is java class file and second is layout XML file. First of you will create a new menu how can create new menu.

Please read step 1 one and step 2 at the above  page.

After creating menu.XML file. you will open a menu.XML file and add a radio group and inside a  group you will add a item and also define id  and title of each item .The behavior that are used in Menu with Radio button are Single None and All .

CheckableBehavior

Single: Used for radio button and select only one option at time

All  :   used for checkbox and select all option

 None: No item or checkbox

For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code .

Code that are write in menu.xml file

After complete this step open a layout.XML file.And add a button and assign id to button and set height and width Wrap_content. Means height and width similar to text that are enter.And define on click method. when user click on button show Menu with Radio button.

Code that are write in Layout. XML file

Code that are write PopupMenuEventHandle. java class file.

For handling the click events onContextItemSelected on is used.To perform an action when the user selects option  Menu with Radio button you must implement onContextItemSelected method. We will get a id of attribute that are define in layout.XMl file  and used switch statement .In a switch case we will used three alternative. if user select first option then first condition or case will execute if user select second option then second condition or case will execute and used Toast.makeText to show message which item user will select.

Run the Application 

Run the application and click on Selection button then Menu with Radio button are show.radio button with option menu in androd

 

Thank you  for reading this lecture Hope you got the idea.