Tuesday, March 16, 2021

Android PopUp Menu

 

Android Popup Menu Example

Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

The android.widget.PopupMenu is the direct subclass of java.lang.Object class.

Android Popup Menu Example

Let's see how to create popup menu in android.

activity_main.xml

It contains only one button.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.popupmenu.MainActivity">  
  8.   
  9.     <Button  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/button"  
  13.         android:text="Click"  
  14.         app:layout_constraintBottom_toBottomOf="parent"  
  15.         app:layout_constraintLeft_toLeftOf="parent"  
  16.         app:layout_constraintRight_toRightOf="parent"  
  17.         app:layout_constraintTop_toTopOf="parent" />  
  18.   
  19. </android.support.constraint.ConstraintLayout>  

popup_menu.xml

It contains three items as show below. It is created inside the res/menu directory.

File: poupup_menu.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:id="@+id/one"  
  5.         android:title="One" />  
  6.     <item  
  7.         android:id="@+id/two"  
  8.         android:title="Two"/>  
  9.     <item  
  10.         android:id="@+id/three"  
  11.         android:title="Three"/>  
  12. </menu>  

Activity class

It displays the popup menu on button click.

File: MainActivity.java
  1. package example.javatpoint.com.popupmenu;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.MenuItem;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.PopupMenu;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12. Button button;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         button = (Button) findViewById(R.id.button);  
  20.         button.setOnClickListener(new View.OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 //Creating the instance of PopupMenu  
  25.                 PopupMenu popup = new PopupMenu(MainActivity.this, button);  
  26.                 //Inflating the Popup using xml file  
  27.                 popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
  28.   
  29.                 //registering popup with OnMenuItemClickListener  
  30.                 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
  31.                     public boolean onMenuItemClick(MenuItem item) {  
  32.                         Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();  
  33.                         return true;  
  34.                     }  
  35.                 });  
  36.   
  37.                 popup.show();//showing popup menu  
  38.             }  
  39.         });//closing the setOnClickListener method  
  40.     }  
  41. }  


Output:

android popup menu example output 1 android popup menu example output 2 android popup menu example output 3

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile