1

I have activity with custom actionbar(Basically custom layout xml), therefore I have created a custom menu button as well in layout file and showing a popup,

I am usingthis callback to showing same popup on tap of hardware option menu, but give a weird behaviour e.g this will open custom menu whenvery onresume is run.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //return super.onCreateOptionsMenu(menu);
        showHideMenu();
        return false;
    }

can you please suggest where I can placeshowHideMenu(); will which work like custom option menu

//showHideMenu() code

private void showHideMenu() {
        if (mPopUpMenu.isShowing())
            dismissPopUpMenu();
        else
            showMenu();
    }

private void dismissPopUpMenu() {
        // dismiss menu
        if (mPopUpMenu != null)
            mPopUpMenu.dismiss();
    }
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85

2 Answers2

1

You should look for an up keyevent:

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        showHideMenu();
        return true;
    }
    return super.onKeyUp(keyCode, event);
} 

Include this for PopUp:

mPopUpMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {        
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode ==  KeyEvent.KEYCODE_MENU) {
                // ... payload action here. e.g. popupMenu.dismiss();
                return true;
            }                
            return false;
        }
    });

Reference: Detecting physical Menu key press in Android

Community
  • 1
  • 1
Shahzeb
  • 3,696
  • 4
  • 28
  • 47
  • this is working fine, but when I press it again, this code does not trigger, it might be because a custom popup is showing that's why onKeyUp is not triggering again for hiding purposes. have you any idea ? – Lavekush Agrawal Oct 05 '15 at 05:10
  • may be you have not handled this case in showHideMenu();. Kindly post this function – Shahzeb Oct 05 '15 at 05:13
  • I have handle but onKeyUp method is not triggering while any popup is displaying on the screen, and my menu is also a custom popup. – Lavekush Agrawal Oct 05 '15 at 05:47
  • Added please have a look in question again – Lavekush Agrawal Oct 05 '15 at 06:31
  • One more check, can you place a breakpoint and see if the function executes even if the popup is showing? I think somehow your instance of mPopUpMenu is getting null in dismissPopUpMenu() – Shahzeb Oct 05 '15 at 06:34
  • I have allready did same things, that's why I inform you that code is not triggering due popup is displaying.and mPopUpMenu is a have live instance, i have already cross all above points, thanks for your support, I think problem is that if any popup is displaying then keyUp event is not triggering. – Lavekush Agrawal Oct 05 '15 at 06:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91354/discussion-between-shahzeb-and-lavekush). – Shahzeb Oct 05 '15 at 06:44
0

you can use PopupMenu for showing custom menu. for this you have to find a view to attach menu.and then you can wrote

 //Creating the instance of PopupMenu  
        PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
        //Inflating the Popup using xml file  
        popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu()); 
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
         public boolean onMenuItemClick(MenuItem item) {  
          Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
          return true;  
         }  
        });  

        popup.show();//showing popup menu  

for more information you can see http://www.androidhive.info/2011/09/how-to-create-android-menus/

John smith
  • 1,781
  • 17
  • 27