Search code examples
androidandroid-toolbarandroid-optionsmenu

Overflow Menu icon in Custom Toolbar


I am using a custom toolbar which has a menu icon. Now on clicking this menu icon i want to show the options menu. How can this be done.

I tried adding a onclicklistener to this menu icon

@Override
public void onClick(View v) {


    if(v.getId() == R.id.toolbarMenuIcon){
        openOptionsMenu();
    }

}

This didnt work. Then i added these lines

setSupportActionBar(mBinding.customSelectToolbar.selectionModeToolbar);

in my activitys oncreate() . Also did override

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.selection_mode_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

With this i can see the overflow menu when i click the icon. But the problem is that, it adds the default menu icon also to the tool bar and thus my tool bar has two menu now. How can i have only my custom toolbar icon open the options menu


Solution

  • If you want to customize the default overflow menu icon..

    Use setOverflowIcon method of your toolbar.

    like:

     toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));