Articles → Android → Popup Menu In Android

Popup Menu In Android






Example




  1. Create a menu folder inside the res folder.
  2. Add a file search_option.xml inside the menu folder.
  3. Picture showing adding the search_option.xml file in the menu folder
    Click to Enlarge

  4. Add following code in search_option.xml
  5.         <?xml version="1.0" encoding="utf-8"?><menu
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"><item android:id="@+id/search" android:title="Search" android:icon="@mipmap/ic_launcher_round" /><item android:id="@+id/about" android:title="About" android:icon="@mipmap/ic_launcher" /></menu>


  6. Add a button on layout
  7. Picture showing adding the button in the linear layout
    Click to Enlarge




public void ButtonClicked(View view) {
  PopupMenu popupMenu = new PopupMenu(this, view);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.search_option, popupMenu.getMenu());
  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
      switch (menuItem.getItemId()) {
      case R.id.search:
        Toast.makeText(EditTextDemo.this, "Search Clicked", Toast.LENGTH_LONG).show();
        return true;
      case R.id.about:
        Toast.makeText(EditTextDemo.this, "About Clicked", Toast.LENGTH_LONG).show();
        return true;
      default:
        return false;
      }
    }
  });
  popupMenu.show();
}



Output


Picture showing the output of the Popup menu in android
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, November 24, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250