Search code examples
androidlistviewpopupposition

Android PopupMenu opening on wrong positions


I have a listView where each item has a button that opens a PopupMenu with options.

Most of the time, these menus open at wrong positions (sometimes they open on correct positions), these random opens are not even consistent.

enter image description here

enter image description here

Choosing an option in the Menu does manipulate with the correct item on the position I originally tried to open the Menu

My code:

settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            PopupMenu popup = new PopupMenu(context, settings);
            popup.getMenuInflater().inflate(R.menu.notes_menu, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {

                    String itemTitle = item.getTitle().toString();

                    switch (itemTitle) {

                        case "Add Pictures": 

                            //adding pictures

                        case "Delete":

                            //deleting

                        case "Share":

                            //sharing

                    }

                    return true;
                }
            });

            popup.show(); 


        }
    });

This settings button is located inside the public View getView(final int position, View convertView, ViewGroup parent) method of my listView adapter.

Any help is greatly appreciated! ^^


Solution

  • Create the popup before setting the onClickListener and only call popup.show() inside onClick().