How To Hide Option Menu?
Solution 1:
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
@OverridepublicvoidonCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
MenuItemitem= menu.findItem(R.id.login_id);
item.setVisible(false);//MenuItemitem= 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 .
Solution 2:
In your Activity
after successful login take handle of the menuItem
in ActionBar
and change it's visibility state
getMenu().findItem(R.id.login_menu_id).setVisible(false);
If you're using Toolbar
then it will be like
toolbar.getMenu().findItem(R.id.login_menu_id).setVisible(false);
So setVisible(boolean)
will change the visibility of a menuItem
. Hope this helps
Solution 3:
and via index also we can do in both callback methods onCreateOptionsMenu or onPrepareOptionsMenu
@OverridepublicbooleanonPrepareOptionsMenu(Menu menu) {
if(!(boolean)PrefUtils.getFromPrefs(this,Constants.IS_LOGIN,false)) {
menu.getItem(1/*R.id.nav_settings*/).setVisible(false);
}
returnsuper.onPrepareOptionsMenu(menu);
}
Post a Comment for "How To Hide Option Menu?"