Introduction

In this article, I will show you how to create a pop-up menu Android app using Android Studio. Android Popup menu displays the menu below the anchor text if space is available otherwise, it displays the menu above the anchor text. It disappears if you tap outside the pop-up menu.
Requirements
Steps to be followed
Follow these steps to create a pop-up menu Android app. I have included the source code below.
Step 1
Open Android Studio and start a new Android Studio Project.
Android
Step 2
You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
Android
Step 3
Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role in running the application.
Android
Step 4
Now, add the activity and click the "Next" button.
Android
Step 5
Add Activity name and click "Finish".
Android
Step 6
Go to activity_main.xml. This XML file contains the designing code for your Android app.
Android
The XML code is given below.
  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="abu.popup.MainActivity" >
  7. <TextView
  8. android:id="@+id/textView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="POP UP " />
  12. <Button
  13. android:id="@+id/button1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentLeft="true"
  17. android:layout_alignParentTop="true"
  18. android:layout_marginLeft="62dp"
  19. android:layout_marginTop="50dp"
  20. android:text="Show Popup" />
  21. </RelativeLayout>
Step 6
Go to Main Activity.java. This Java program is the backend language for Android.
Android
The Java code is given below.
  1. package abu.popup;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.PopupMenu;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12. Button button1;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. button1 = (Button) findViewById(R.id.button1);
  18. button1.setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. PopupMenu popup = new PopupMenu(MainActivity.this, button1);
  22. popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
  23. popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  24. public boolean onMenuItemClick(MenuItem item) {
  25. Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
  26. return true;
  27. }
  28. });
  29. popup.show(); }
  30. });
  31. }
  32. }
Step 7
Create a new XML file and name it as “popup_menu.xml”. Add the below code into it.
Android
The menu XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. xmlns:auto="http://schemas.android.com/apk/res-auto">
  4. <item
  5. android:id="@+id/one"
  6. android:title="abu shithik"/>
  7. <item
  8. android:id="@+id/two"
  9. android:title="c# corner"/>
  10. <item
  11. android:id="@+id/three"
  12. android:title="pop up"/>
  13. <item
  14. android:id="@+id/four"
  15. android:title="articles"/>
  16. <item
  17. android:id="@+id/five"
  18. android:title="message"/>
  19. </menu>
Step 8
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
Android
Step 9
Then, click the "Run" button or press shift+f10 to run the project. Choose the "virtual machine" option and click OK.
Android

Conclusion

We have successfully created a pop-up menu Android application using Android Studio.
Android
Android