Wednesday, 14 September 2016

PopUp Menu Example in Android Studio

1. Android PopUp Menu?
Ans:-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.PopupMenu is the direct subclass of java.lang.Object class.


Example of PopUp Menu:
Required class,file,&layouts:-
1.MainActivity.java | activity_main.xml
2.Login.java | login.xml
3.Make Folderinside res:- menu | popup_menu.xml
Step1:-Take new project and add new folder inside res.Right Click on  res->new->Directory->set name menu->ok.Now right click on menu folder and add new layouts for popup menu options. res-.menu->right click on menu->new->Menu resource file->give name popup_menu.xml->ok.

Step2:-popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/itemLogin"
            android:title="Login">
        </item>
    <item
        android:id="@+id/itemSignup"
        android:title="SignUp">
    </item>
    <item
        android:id="@+id/itemPfofile"
        android:title="Profile">
    </item>
    <item
        android:id="@+id/itemSetting"
        android:title="Settings">
    </item>
</menu>
Step3:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="16dp"
    tools:context="supriya.com.popupmenu.MainActivity">

   <Button
       android:layout_marginTop="30dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/btnPopup"
       android:text="Show PopUp Menu"
       android:background="#FE2E64"
       android:textColor="#fff"/>
</LinearLayout>
Step4:-MainActivity.java
package supriya.com.popupmenu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btnPopup;

    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPopup=(Button)findViewById(R.id.btnPopup);
        btnPopup.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                PopupMenu popUpMenu=new PopupMenu(MainActivity.this,view);
                popUpMenu.getMenuInflater().inflate(R.menu.popup_menu,popUpMenu.getMenu());
               popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                   @Override  
                 public boolean onMenuItemClick(MenuItem item) {
                       switch (item.getItemId()){
                           case R.id.itemLogin:
                               startActivity(new Intent(MainActivity.this,Login.class));
                               Toast.makeText(getApplicationContext(),"Login",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemSignup:
                               Toast.makeText(getApplicationContext(),"Signup",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemPfofile:
                               Toast.makeText(getApplicationContext(),"Profile",Toast.LENGTH_LONG).show();
                               return true;

                           case R.id.itemSetting:
                               Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_LONG).show();
                               return true;
                       }

                       return true;
                   }
               });
                popUpMenu.show();
            }
        });
    }
}
Step5:-login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_margin="16dp"     android:gravity="center_vertical">     <EditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Enter Email"/>     <EditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="Password"/> </LinearLayout> Step6:-Login.java
package supriya.com.popupmenu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/** * Created by Supriya on 9/14/2016. */public class Login extends AppCompatActivity {
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}
Output:-



No comments:

Post a Comment