Detect click on Actionbar's Overflow menu button

前端 未结 4 664
逝去的感伤
逝去的感伤 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:46

    If you have setup Toolbar and inflated menu correctly, just use these two function in MainActivity

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        //Perform some action on menu open
        return super.onMenuOpened(featureId, menu);
    }
    
    @Override
    public void onPanelClosed(int featureId, Menu menu) {
        //Perform some action on menu closed
    }
    

    Setup toolbar in onCreate function of MainActivity

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    And inflate your menu which is defined in xml file

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }
    

    my_menu.xml

    
    
        
        
    
    

提交回复
热议问题