How to use Android popup menu as Android options menu?


Download popup menu as Android options menu sample

Android floating options menu doesn't support icon. In this example I have demonstrated how we can use an Android popup menu as Android options menu. As we know android options menu doesn't allow us to dispaly icons with text, so sometimes it become important to use android popup menu as Android options menu with icons and text.

Android popup menu as Options menu


Example


Step 1: Create a new project in Android Studio and name it Android options Menu with icon .

Step 2: Create a folder menu inside res directory if it is not already present.

Step 3: Open res -> menu and add a new menu resource file with a name popup_menu.xml. Now, replace the code of popup_menu.xml with the below given code.

popup_menu.xml

Step 4: Open res -> menu and add a new menu resource file with a name option_menu.xml. Now, replace the code of option_menu.xml with the below given code.

Note: Please note that I have defined onClick attribute of item element here. So, we'll implement a method in our activity with same name that will handle click event of this item.

option_menu.xml

Step 5: Open MainActivity.java and replace its code with below given code.

MainActivity.java

Step 6:Clean and run your project to see the result.

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.