How to Set PopupMenu in Android

PopupMenu

A PopupMenu displays a Menu in a model popup window anchored to a View.

Procedure

  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml for layout design.
  5. Take a button and a textview in xml file.
  6. Lookup the button in java file.
  7. In onClick function Declare PopupMenu like this:

PopupMenu pi=new PopupMenu(MainActivity.this, v);
pi.inflate(R.menu.main);
pi.setOnMenuItemClickListener
(new OnMenuItemClickListener()
{
    public boolean onMenuItemClick(MenuItem vv)
    {
         switch (vv.getItemId())
         {
              case R.id.action_settings:
             Toast.makeText(getApplicationContext(),
              "Abhijeet", 100).show();
              break;
              default:
              break;
          }
          return false;
      }
});
pi.show();
}
}

Code is given below

MainActivity.java

package com.example.popupmenuuuuu;
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;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class MainActivity extends Activity
implements OnClickListener
{
     Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(this);
    }
    public void onClick(View v)
   {
      // TODO Auto-generated method stub
      PopupMenu pi=new PopupMenu(MainActivity.this, v);
       pi.inflate(R.menu.main);
       pi.setOnMenuItemClickListener
       (new OnMenuItemClickListener()
       {
            public boolean onMenuItemClick(MenuItem vv)
            {
                 // TODO Auto-generated method stub
                 switch (vv.getItemId())
                 {
                      case R.id.action_settings:
                      Toast.makeText(getApplicationContext(),
                      "Abhijeet", 100).show();
                       break;
                       default:
                       break;
                   }
                    return false;
              }
      });
      pi.show();
   }
}

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=".MainActivity" >

 
<TextView

  android:id="@+id/textView1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="PopupMenu Demo" />

 

<Button

  android:id="@+id/button1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignLeft="@+id/textView1"

  android:layout_below="@+id/textView1"

  android:layout_marginLeft="36dp"

  android:layout_marginTop="27dp"

  android:text="Click Here" />
</RelativeLayout>

 

Output

 
 
 
 
Ebook Download
View all
Learn
View all