Detect click on Actionbar's Overflow menu button

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

    Finally i found the solution. Override FragmentActivity.onKeyDown

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
    
        case KeyEvent.KEYCODE_MENU:
            // Do Sometihng
            break;
    
        default:
            break;
        }
        return super.onKeyDown(keyCode, event);
    } 
    
    0 讨论(0)
  • 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;
        }
        ...
    }
    
    0 讨论(0)
  • 2020-12-10 08:43

    To detect the click on the overflow menu have such code:

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR && menu != null){
            //overflow menu clicked, put code here...
        }
        return super.onMenuOpened(featureId, menu);
    }
    
    @Override
    public void onPanelClosed(int featureId, Menu menu) {
        ...
    }
    

    To detect click on menu items, in case you have a menu like that :

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/menu2" android:alphabeticShortcut="b"
            android:title="Menu No. 2" android:orderInCategory="2">
            <menu >
            <group android:id="@+id/group2" android:checkableBehavior="single">
                <item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
                <item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
            </group>  
            </menu>
        </item>
    </menu>
    

    You should be able to detect the click in

    onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        Log.w("ANDROID MENU TUTORIAL:", "onOptionsItemSelected(MenuItem item)");
    
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu2:
            Toast.makeText(this, "Clicked: Menu No. 2", Toast.LENGTH_SHORT).show();
            return true;   
            ...
    }
    
    0 讨论(0)
  • 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

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/action_something"
            android:title="@string/something_text" />
        <!--Other menu items-->
    </menu>
    
    0 讨论(0)
提交回复
热议问题