Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide option menu?

Tags:

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

like image 318
Krishnan Avatar asked Aug 24 '15 06:08

Krishnan


People also ask

How can I hide menu items in Android?

Hide button by default in menu xml By default the share button will be hidden, as set by android:visible="false" .

How do we hide the menu on the toolbar in one fragment?

When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment. The fragment menu items disappear from the action bar also.

What is invalidateOptionsMenu in Android?

invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu() , so that the system could redraw it on UI.


1 Answers

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

android:visible="false" 

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

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {      inflater.inflate(R.menu.menu, menu);      MenuItem item = menu.findItem(R.id.login_id);             item.setVisible(false);//     MenuItem item = menu.findItem(R.id.logOff_id);             item.setVisible(true);     super.onCreateOptionsMenu(menu, inflater); } 

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

like image 92
AKASH WANGALWAR Avatar answered Oct 20 '22 11:10

AKASH WANGALWAR