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 ?

5
  • 1
    Possible duplicate of How to create a drop-down list? Commented Nov 4, 2016 at 10:25
  • 1
    Why does it have to be programatically? Commented Nov 4, 2016 at 10:28
  • @MidasLefko : I added above to rows dynamically . So other components should be generated dynamically . Commented Nov 4, 2016 at 10:33
  • without using xml will make this complex, why do you need it to be done programmatically? Commented Nov 4, 2016 at 10:38
  • what are the other components? Commented Nov 4, 2016 at 10:40

3 Answers 3

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();
        }
    });
Sign up to request clarification or add additional context in comments.

4 Comments

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 .
@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."
@Barrier If so - add some space below list's last view. Need more info to help you.
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>

Comments

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

1 Comment

Thanks I will try on this and inform you later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.