Click a button to have the menu option pop-up from the specified position around the button.

Source: Internet
Author: User

Android Material Design:popupmenu

Android Material Design introduced popupmenu similar to the past context menu, but more flexible. :

Now give the code to implement PopupMenu. This example is a normal button triggering pop-up popupmenu.

Mainactivity.java of the test:

[Java]View Plaincopyprint?
  1. Package zhangphil.materialdesign;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.support.v7.widget.PopupMenu;
  5. Import Android.view.MenuItem;
  6. Import Android.view.View;
  7. Import Android.widget.Button;
  8. Import Android.widget.Toast;
  9. Public class Mainactivity extends Activity {
  10. @Override
  11. protected void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.activity_main);
  14. Final Button button = (button) Findviewbyid (R.id.button);
  15. Button.setonclicklistener (new View.onclicklistener () {
  16. @Override
  17. public void OnClick (View v) {
  18. ShowPopupMenu (button);
  19. }
  20. });
  21. }
  22. private void ShowPopupMenu (view view) {
  23. //view current PopupMenu The location of the relative view displayed
  24. PopupMenu PopupMenu = new PopupMenu (this, view);
  25. //Menu Layout
  26. Popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ());
  27. //menu item Click event
  28. Popupmenu.setonmenuitemclicklistener (new Popupmenu.onmenuitemclicklistener () {
  29. @Override
  30. Public Boolean Onmenuitemclick (MenuItem item) {
  31. Toast.maketext (Getapplicationcontext (), Item.gettitle (), Toast.length_short). Show ();
  32. return false;
  33. }
  34. });
  35. //PopupMenu Shutdown Event
  36. Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {
  37. @Override
  38. public void Ondismiss (PopupMenu menu) {
  39. Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();
  40. }
  41. });
  42. Popupmenu.show ();
  43. }
  44. }
Package Zhangphil.materialdesign;import Android.app.activity;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 Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Final button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {showpopupmenu (button);}}); private void ShowPopupMenu (view view) {///view current PopupMenu Displays the location of the relative View popupmenu popupmenu = new PopupMenu (this, View);//M ENU layout popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ());// menu item Click event Popupmenu.setonmenuitemclicklistener (New Popupmenu.onmenuitemclicklistener () {@Overridepublic Boolean Onmenuitemclick (MenuItem item) {Toast.maketext (Getapplicationcontext (), Item.gettitle (), toAst. Length_short). Show (); return false;}); /PopupMenu Shutdown Event Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {@Overridepublic void Ondismiss ( PopupMenu menu) {Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();}); Popupmenu.show ();}}

The Activity_main layout file required by Mainactivity.java contains only one normal Android Button, which is not mentioned again.

Main.xml in the menu directory:

[HTML]View Plaincopyprint?
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="Http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="Http://schemas.android.com/tools" >
  4. <item
  5. android:id="@+id/action_open"
  6. android:orderincategory="
  7. android:title="open"
  8. app:showasaction="never"/>
  9. <item
  10. android:id="@+id/action_new"
  11. android:orderincategory="
  12. android:title="new"
  13. app:showasaction="never"/>
  14. <item
  15. android:id="@+id/action_del"
  16. android:orderincategory="
  17. android:title="Delete"
  18. app:showasaction="never"/>
  19. </Menu>

Click a button to have the menu option pop-up from the specified position around the button.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.