Quantcast
Viewing all articles
Browse latest Browse all 10

Popup Menu in android studio

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->

Image may be NSFW.
Clik here to view.
Alt Tag android add  new menu.xml image

Step2=Step1<-Writename->ok

Image may be NSFW.
Clik here to view.
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

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/id_admin" android:title="login AS admin"></item>
    <item android:id="@+id/id_user" android:title="login AS user"></item>

</menu>

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"
        android:id="@+id/button"
        android:onClick="showPopUp"
        android:layout_marginTop="169dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Code that are write MainActivity. java class file.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void showPopUp(View view){
        PopupMenu popupMenu=new PopupMenu(this,view);
        MenuInflater menuInflater=popupMenu.getMenuInflater();
        PopUpMenuEventHandle popUpMenuEventHandle=new PopUpMenuEventHandle(getApplicationContext());
        popupMenu.setOnMenuItemClickListener(popUpMenuEventHandle);
        menuInflater.inflate(R.menu.popup_action,popupMenu.getMenu());
        popupMenu.show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

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.  

Image may be NSFW.
Clik here to view.
Alt Tag Android make new class

Step3=Step2->WriteName->Ok

Image may be NSFW.
Clik here to view.
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.

public class PopUpMenuEventHandle implements PopupMenu.OnMenuItemClickListener {
    Context context;
    public PopUpMenuEventHandle(Context context){
        this.context =context;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId()==R.id.id_admin) {
            Toast.makeText(context,"Login is admin",Toast.LENGTH_SHORT).show();
            return true;
        }
       if (item.getItemId()==R.id.id_user){

            Toast.makeText(context,"login is user",Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

Run the Application 

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

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
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

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:id="@+id/id_Wifi" android:title="use wifi"></item>
        <item android:id="@+id/id_bluetooth" android:title="use Bluetooth"></item>
        <item android:id="@+id/id_mobile_data" android:title="use Mobile Data"></item>
    </group>

</menu>

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Selection Button"
        android:id="@+id/button"
        android:onClick="selectNetwork"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp" />
</RelativeLayout>

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.

package com.example.bmw.buttonradioclick;

import android.support.v7.app.ActionBarActivity;
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.Toast;


public class MainActivity extends ActionBarActivity {
    int item_selection=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void selectNetwork(View view){
        registerForContextMenu(view);
        openContextMenu(view);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.network_menu,menu);
        MenuItem item_wifi= menu.findItem(R.id.id_Wifi);
        MenuItem item_bluetooth= menu.findItem(R.id.id_bluetooth);
        MenuItem item_mobile_data= menu.findItem(R.id.id_mobile_data);
        if (item_selection==1){
            item_wifi.setChecked(true);
        }
        if (item_selection==2){
            item_bluetooth.setChecked(true);
        }
        if (item_selection==3){
            item_mobile_data.setChecked(true);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.id_Wifi:
                Toast.makeText(getApplicationContext(),"Wifi is selected",Toast.LENGTH_SHORT).show();
                item.setChecked(true);
                item_selection=1;
                return true;
            case R.id.id_bluetooth:
                Toast.makeText(getApplicationContext(),"Bluetooth is selected",Toast.LENGTH_SHORT).show();
                item.setChecked(true);
                item_selection=2;
                return true;
            case R.id.id_mobile_data:
                Toast.makeText(getApplicationContext(),"Mobile is selected",Toast.LENGTH_SHORT).show();
                item.setChecked(true);
                item_selection=3;
                return true;

        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Run the Application 

Run the application and click on Selection button then Menu with Radio button are show.Image may be NSFW.
Clik here to view.
radio button with option menu in androd

 

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

The post Popup Menu in android studio appeared first on The Master World.


Viewing all articles
Browse latest Browse all 10

Trending Articles