Pop Up Menu Android Application

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.   
  8.     <TextView  
  9.   
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="POP UP " />  
  14.   
  15.     <Button  
  16.         android:id="@+id/button1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_marginLeft="62dp"  
  22.         android:layout_marginTop="50dp"  
  23.         android:text="Show Popup" />  
  24. </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.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         button1 = (Button) findViewById(R.id.button1);  
  20.         button1.setOnClickListener(new OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.      
  25.                 PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
  26.                   
  27.                 popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
  28.   
  29.                 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
  30.                     public boolean onMenuItemClick(MenuItem item) {  
  31.                         Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
  32.                         return true;  
  33.                     }  
  34.                 });  
  35.   
  36.                 popup.show();            }  
  37.         });   
  38.     }  
  39. }  
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.   
  8.     <item  
  9.         android:id="@+id/two"  
  10.         android:title="c# corner"/>  
  11.   
  12.     <item  
  13.         android:id="@+id/three"  
  14.         android:title="pop up"/>  
  15.   
  16.     <item  
  17.         android:id="@+id/four"  
  18.         android:title="articles"/>  
  19.   
  20.     <item  
  21.         android:id="@+id/five"  
  22.         android:title="message"/>  
  23.   
  24. </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


Similar Articles