2

I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.

NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.

enter image description here

I have no idea about this .

Have any ideas ?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61

3 Answers3

4

not perfect, but it works ;)

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
            popupMenu.getMenu().add("Edit");
            popupMenu.getMenu().add("Delete");
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getTitle().toString()) {
                        case "Edit" :
                            //execute "edit" action
                            break;
                        case "Delete" :
                            //execute "delete" action
                            break;
                    }
                    return false;
                }
            });
            popupMenu.show();
        }
    });
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
  • Thanks for the answer . But I have a problem with using this . Last popup menu is loading above the button . How could I stop it . I've tried setting Gravity , but it didn't work . – Terance Wijesuriya Nov 04 '16 at 11:32
  • Please check the http://stackoverflow.com/questions/34565481/add-a-drop-down-menu-for-each-item-of-a-custom-listview – Manikandan K Nov 04 '16 at 11:45
  • @Barrier Glad to be helpful! If there is enough space below the anchor button? From documentation: "The popup will appear below the anchor if there is room, or above it if there is not." – Maksim Ostrovidov Nov 04 '16 at 12:24
  • @Barrier If so - add some space below list's last view. Need more info to help you. – Maksim Ostrovidov Nov 04 '16 at 12:32
2

I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.

In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);

At this point since you need dynamic menu items you have the following options:

  1. You can call PopopMenu.getMenu() and manually populate it with MenuItems
  2. You can create an xml menu resource and then adjust/hide ones that need to be changed
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
2
Just try to check and implement it

    PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow); 
    overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu()); 

    overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
         public boolean onMenuItemClick(android.view.MenuItem item) {
                   switch (item.getItemId()) {
                          case R.id.edit: 
                               break;
                           case R.id.delete:  
                                break; 
                     }
                           return true;
                 }
   });
  overflowPopupMenu.show();

popup_overflow_options.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:id="@+id/edit" 
        android:title="@string/edit"/>
    <item
        android:id="@+id/delete" 
        android:title="@string/delete"/> 
</menu>
Manikandan K
  • 911
  • 9
  • 21