1

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

2
  • it adds the default menu icon?...do you add a custom menu icon in your toolbar? Commented Dec 14, 2017 at 15:49
  • yes i have an icon toolbarMenuIcon in my toolbar layout. I want to show the options menu on clicking this one Commented Dec 14, 2017 at 15:53

2 Answers 2

4

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));
Sign up to request clarification or add additional context in comments.

5 Comments

i have all other icons on toolbar also customised. So is it not possible to have a button that act as options menu icon on click
sounds like you want something like This
Also your question is not clear...if you set the custom icon as your overflow menu....it will open the options as well !..you need to override the onOptionsItemSelected(MenuItem item)...to to define the onClick behaviour of your MenuItem..
Thanks for your inputs. I dont want to show a popup menu. "if you set the custom icon as your overflow menu....it will open the options as well !." This is not happening if i just call openOptionsMenu without setting setSupportActionBar(mytoolbar) . The oncreateOptionsmenu is never getting triggered in that case
the 'oncreateOptionsmenu` is the method of Actionbar by calling setSupportActionBar(mytoolbar) we enable the toolbar as actionbar..without it it will never work, also i don't figure out what is the problem by setSupportActionBar(mytoolbar)? also this is out of topic in your question..u better repost ur question again..!
1

I got it working using theme

<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_menu_overflow</item>
    <item name="android:tint">...</item>
    <item name="android:width">..dp</item>
    <item name="android:height">..dp</item>

</style>

<style name="OnArrival.toolbarTheme" parent="Theme.OnArrival.Light">

    <item name="actionOverflowButtonStyle">@style/OverFlow</item>
</style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.