Android Popup Menu Example

AndroidMobile DevelopmentApps/Applications

Popup menu just like a menu, it going to be display either above of the view or below of the view according to space on activity. Here is the simple solution to create android popup menu.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:tools = "http://schemas.android.com/tools"
   android:id = "@+id/rootview"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   android:background = "#c1c1c1"
   android:gravity = "center_horizontal"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/popup"
      android:text = "Download"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

In the above code, we have given button. when you click on the above button, it going to show popup menu.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   Button popupButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      popupButton = findViewById(R.id.popup);
      popupButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            popupMenuExample();
         }
      });
   }
   private void popupMenuExample() {
      PopupMenu p = new PopupMenu(MainActivity.this, popupButton);
      p.getMenuInflater().inflate(R.menu.popup_menu_example, p .getMenu());
      p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
         public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(MainActivity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
         }
      });
      p.show();
   }
}

In the above code when you click on button it going to create popup menu object and add to menu as shown below -

PopupMenu p = new PopupMenu(MainActivity.this, popupButton);
p.getMenuInflater().inflate(R.menu.popup_menu_example, p .getMenu());

In the above code we have inflate menu as popup_menu_example as shown below -

<?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/java"
      android:title = "JAVA"/>
   <item
      android:id = "@+id/kotlin"
      android:title = "Kotlin"/>
</menu>

When user click on the menu item it will call onMenuItemClickListener() as shown below -

p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
   public boolean onMenuItemClick(MenuItem item) {
      Toast.makeText(MainActivity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
      return true;
   }
});

To show the popup menu we have to call show() as shown below -

p.show();

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

When you click on download button it will show popup menu. Button doesn't have space on the above so it will show at bottom. as shown below -

Now click on any item it will give message as shown below -

Click here to download the project code

raja
Published on 31-Jan-2019 12:20:58
Advertisements