Create custom Popup Menu in Android Studio Eclipse example tutorial

How to show and get menu value using custom menu.xml file with setOnMenuItemClickListener() method.

Pop Up menu is used to show custom menus inside android applications. PopUp menu shows on button click but created and call from custom XML file that is put inside Menu folder of your project. So basically we can say that all the items of your Menu are directly called from XML file which is located inside Menu folder on button click. So here is the complete step by step tutorial for Create custom Popup Menu in Android Studio example tutorial.

android-project-download-code-button

Note : Please create pop_up_menu.xml file inside your project’s menu folder.

menu-folder

How to Create custom Popup Menu in Android Studio example tutorial.

Code for MainActivity.java file.

package com.popupmenu_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity {

 Button button;
 PopupMenu popupmenu ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 PopMenuDisplay();
 
 }
 });
 }
 
 public void PopMenuDisplay() {
 
 popupmenu = new PopupMenu(MainActivity.this, button);
 
 popupmenu.getMenuInflater().inflate(R.menu.pop_up_menu, popupmenu.getMenu());
 
 popupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
 
 public boolean onMenuItemClick(MenuItem item) {
 
 Toast.makeText(MainActivity.this, item.getTitle(),Toast.LENGTH_LONG).show();
 
 return true;
 }
 });
 
 popupmenu.show();
 
 }
}

Code for activity_main.xml layout file.

<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.popupmenu_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="143dp"
 android:text="Show Pop Up Menu" />

</RelativeLayout>

Code for pop_up_menu.xml file.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
 
 <item
 android:id="@+id/android"
 android:title="Android"
 />
 
 <item
 android:id="@+id/Php"
 android:title="PHP"
 />
 
 <item
 android:id="@+id/Blogger"
 android:title="Blogger"
 />
 
 <item
 android:id="@+id/WordPress"
 android:title="WordPress"
 />

</menu>

Screenshot:

Create custom Popup Menu in Android Studio example tutorial

Click here to download Create custom Popup Menu in Android Studio example tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *