Open top menu


 Explain How to create  Popup Menu in Android.


A Popup Menu displays a menu in a modal popup window anchored to a View. The popup will appear below the anchor view .  the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.
The menu show via main.xml class ,show below example...

My UI class  activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidbeginner.testcode.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:text="POPUP MENU" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="Androidbeginner" 
        android:textSize="20sp"/>

</RelativeLayout>

My Menu class main.xml.


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.androidbeginner.testcode.MainActivity" >

   <item  
        android:id="@+id/one"  
        android:title="One"/>  
      
    <item  
        android:id="@+id/two"  
        android:title="Two"/>  
        
    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>



My Actvity class Main_Activity.java

package com.androidbeginner.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
PopupMenu popup;
Button butnpoup;  
     
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        butnpoup = (Button) findViewById(R.id.button1);  
        butnpoup.setOnClickListener(this);
    }
@Override
public void onClick(View v) {
PopupMenu();
public void PopupMenu() {
PopupMenu popup = new PopupMenu(MainActivity.this, butnpoup);  
         //Inflating the Popup using xml file  
         popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());  
        
         //registering popup with OnMenuItemClickListener  
         popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
          public boolean onMenuItemClick(MenuItem item) {  
           Toast.makeText(MainActivity.this,"You Clicked : " +                                                                                                        
item.getTitle(),Toast.LENGTH_SHORT).show();  
           return true;  
          }  
         });  

         popup.show();
          }  
           
 
}

0 comments