Android popup menu

Besides context and option menus, you can use the popup menu in your Android applications. The popup menu is useful when you want to provide a drop-down list of actions similar to Spinner or an overflow style-menu. The popup menu is anchored to a view. It is displayed below the view if there is room; otherwise it is above the view.


To show a popup menu, first you need to construct an object of the PopupMenu class with its constructor that takes the current context, and the view which the popup menu is anchored. Then you can call the inflate() Method of the MenuInflater object returned by the getMenuInflater() method of the PopupMenu object to inflate a popup menu xml file.  




To perform an action when the user selects an item of the popup menu, you need to call setOnMenuItemClickListener() method of the PopupMenu object and pass to this method an anonymous object that implements the OnMenuItemClickListener interface. You have to override the onMenuItemClick() method.
In this example, you will create a popup menu file called popupmenu.xml in the res/menu folder of your current project.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

   <item
     android:id="@+id/itemshare"
     android:orderInCategory="100"
     android:showAsAction="always"
     android:title="Share"/>
   <item
     android:id="@+id/itempremove"
     android:orderInCategory="100"
     android:showAsAction="always"
     android:title="Remove"/>

</menu>

Then modify the activity_main.xml file to add a button. The button is clicked to display the popup menu.

<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"
   tools:context="com.example.androidexample.MainActivity"
   android:orientation="vertical"
   >
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Show popup menu"
     android:onClick="showPopupMenu"
     />

</LinearLayout>


Here is the code in the MainActivity class to inflate the popupmenu.xml file and handle the menu item click-event.

package com.example.androidexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
   Activity context;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     context=this;
    
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main, menu);
     return true;
   }
  
   public void showPopupMenu(View view){
     PopupMenu popup = new PopupMenu(this, view);
     MenuInflater inflater = popup.getMenuInflater();
     inflater.inflate(R.menu.popupmenu, popup.getMenu());
     popup.setOnMenuItemClickListener(new OnMenuItemClickListener(){
    
     @Override
     public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.itemshare:
             Toast.makeText(context, "Action: "+item.getTitle(), Toast.LENGTH_SHORT).show();
             return true;
          case R.id.itempremove:
          Toast.makeText(context, "Action: "+item.getTitle(), Toast.LENGTH_SHORT).show();
             return true;
          default:
             return false;
        }
     }
     });
     popup.show();
   }
}

Android-popup-menu

Posted by: Dara | post date: 09-30-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.