Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Popup menu when list item view pressed?

i would like to implement a popup menu similar to google's play store as shown below.

enter image description here

so basically from what i understand, i'll need an activity and a layout for this activity with a listview defined in it. i need to create my custom adapter. also, i need to create a list layout would contain the information and a view (with the 3 dots) that will serve as the button to launch the popup menu? the issue that i'm seeing here is that how do i create a listener for this view only and how do i reference the value for that specific list item in the list view.

i don't have any code available yet as i haven't started anything related to this. i'm currently getting info in theory for now but if required i will create a sample code.

thanks.

like image 450
user2469412 Avatar asked Sep 03 '13 09:09

user2469412


People also ask

How do I open the pop-up menu on Android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.

When user presses a view for a long time which menu can be shown to user?

In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time.

What are the two types of popup menus?

PopupMenu - A modal menu that is anchored to a particular view within an activity. The menu appears below that view when it is selected. Used to provide an overflow menu that allows for secondary actions on an item. PopupWindow - A simple dialog box that gains focus when appearing on screen.

How do I pass custom layout to PopupMenu?

OnClickListener() { @Override public void onClick(View view) { PopupMenu popupMenu = new PopupMenu(Sample1. this, view); popupMenu. setOnMenuItemClickListener(Sample1. this); popupMenu.


2 Answers

Using popup menu it's quite simple to create a menu with these three steps:

1 - Add a click listener to the menu button using OnClickListener or as i prefer from the layout xml:

<ImageButton android:id="@+id/menu_button" android:onClick="showMenu" ... />

2 - Create the menu layout menu_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item_settings"
        android:showAsAction="ifRoom|withText"
        android:title="Settings"
        android:visible="true"/>
    <item
        android:id="@+id/item_about"
        android:showAsAction="ifRoom|withText"
        android:title="About"
        android:visible="true"/>
</menu>

3 - Create a popup menu, inflate the xml layout and show it:

public void showMenu (View view)
{
    PopupMenu menu = new PopupMenu (this, view);
    menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()
    {
        @Override
        public boolean onMenuItemClick (MenuItem item)
        {
            int id = item.getItemId();
            switch (id)
            {
                case R.id.item_settings: Log.i (Tag, "settings"); break;
                case R.id.item_about: Log.i (Tag, "about"); break;
            }
            return true;
        }
    });
    menu.inflate (R.menu.menu_layout);
    menu.show();
}
like image 192
razz Avatar answered Sep 29 '22 06:09

razz


ActionBarCompat List PopupMenu implementation is here (with back port available because it uses ABC)!

enter image description here

You can also get this sample from Github or from SDK (Mr.Morgan commented below)

/sdk/samples/android-19/ui/ActionBarCompat-ListPopupMenu. Make sure to install Samples for SDK under Android 4.4.2 (API 19)

like image 29
LOG_TAG Avatar answered Sep 29 '22 08:09

LOG_TAG