Detect click on Actionbar's Overflow menu button

前端 未结 4 668
逝去的感伤
逝去的感伤 2020-12-10 07:42

Can I detect click/tap on the menu button of action bar, i.e. used to show overflow menu items?

By default it opens up the list with one item \"Settings\". Here is t

4条回答
  •  难免孤独
    2020-12-10 08:35

    By default clicking on the overflow button shows the options menu, so i believe you should manage to intercept the event, then do what you want by overriding Activity.onPrepareOptionsMenu

    This was a little tricky since i couldn't find an id for overflow button, so i used this hack. In your Activity :

    private boolean actionBarClicked = false;
    
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        if (item.getId() == )
            actionBarClicked = true;
        return false; // Let default processing occur
    }
    
    @Override
    public boolean onPrepareOptionsMenu (Menu menu) {
        if (actionBarClicked) {
            // Overflow button of ActionBar was clicked, do what you want here.
            actionBarClicked = false;
        }
        ...
    }
    

提交回复
热议问题