2

I am using popup menu in my code. It works perfectly . but when I select an item from it, menu was not dismissing . I tried this . but it is not working

here is my code

 PopupMenu popup = new PopupMenu(this, edit1);
        //inflating menu from xml resource
        popup.inflate(R.menu.options_menu);
        popup.getMenu().add("one");
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // if (item.getTitle().equals("one")) {
                Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();

        popup.dismiss();
        return false;
            }
        });
        //displaying the popup
        popup.show();

Please some one help me.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
basha
  • 587
  • 2
  • 6
  • 25

2 Answers2

3

try this below code it will work for you

 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // if (item.getTitle().equals("one")) {
                Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                popup.dismiss();

                return true;
            }
        });

and as I see your comments if you are using onTouchListener for the EditText. just change it to onClickListener and your problem will be solved.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
  • it is not working . again same issue after clicking four times it is dismissing. – basha Jan 22 '18 at 09:18
  • you need to put id in if else condition then it will work – Sachin Rajput Jan 22 '18 at 09:19
  • can you give you menu.xml code in your question , so i can help you with appropriate code – Sachin Rajput Jan 22 '18 at 09:19
  • My menu file is empty . I actually added menu items as popup.getMenu().add("one"); – basha Jan 22 '18 at 09:20
  • I just debug my code, problem is from my side. don't know why my method caling 4 times .will check – basha Jan 22 '18 at 09:23
  • check my updated answer , and as I see your comments if you are using **onTouchListener** for the EditText. just change it to onClickListener and your problem will be solved. – Sachin Rajput Jan 22 '18 at 09:24
  • 1
    basha - onTouchListener responds for both landing and lifting , you need to replace it with onClickListener and then use the code as I written over here , it will solve your problem – Sachin Rajput Jan 22 '18 at 09:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163619/discussion-between-thunder-and-basha). – Sachin Rajput Jan 22 '18 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163634/discussion-between-thunder-and-basha). – Sachin Rajput Jan 22 '18 at 11:01
0

This should be the code block. Returning true shows that the click was handled.

@Override
public boolean onMenuItemClick(MenuItem item) {
   // if (item.getTitle().equals("one")) {
   Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
   return true;
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57