android – How to hide option menu?-ThrowExceptions

Exception or error:

I am using option selected menu item, attached three item (share, login, logout) am select login item, go login activity login social sign (Facebook or google) any one, if login hide login item at same time show logout item, this same type if logout the social sign show login item menu, please help me…

enter image description here

How to solve:

step:1) menu.xml define all three menu item. login ,logout and share after that make logout visibility to false by default


and make remaining two items visible.its optional because by default all items are visible in android

Step:2)when you are in login Activity inflate that xml.and no need to make any change in activity
at these point we are showing login and share menu item only and we have already made logOff item visibility to false in the xml .

step:3) when you are in main activity(activity that you are showing after login activity) do these

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(, menu);

    MenuItem item = menu.findItem(;
    MenuItem item = menu.findItem(;
    super.onCreateOptionsMenu(menu, inflater);

at these point you will get logOff and share because we have made login menu item visibility to false .


In your Activity after successful login take handle of the menuItem in ActionBar and change it’s visibility state


If you’re using Toolbar then it will be like


So setVisible(boolean) will change the visibility of a menuItem. Hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *