27

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

Krishnan
  • 428
  • 1
  • 4
  • 11
  • Some good answers in this post: [https://stackoverflow.com/a/68490157/7348019](https://stackoverflow.com/a/68490157/7348019) – Michael Jul 23 '21 at 13:18

3 Answers3

33

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 .

AKASH WANGALWAR
  • 1,326
  • 1
  • 14
  • 21
15

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

Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
  • Am asking if login Facebook or google plus any one, hide login item, at same time show logout menu item – Krishnan Aug 27 '15 at 10:04
  • Yeah u can do with the same above code! What's the difficulties you're facing? – Kavin Prabhu Aug 27 '15 at 10:14
  • Hi Kevin, am using two activity main activity and login activity, to create menu item in main activity (menus : share, login, logout) we go login acitvity -> am set facebook and google plus, am if facebook login go to main activity using "registercallback method" via intent to main activity, it same to google plus, my problem for menu item not function – Krishnan Aug 28 '15 at 07:07
0

and via index also we can do in both callback methods onCreateOptionsMenu or onPrepareOptionsMenu

 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
     if(!(boolean)PrefUtils.getFromPrefs(this,Constants.IS_LOGIN,false)) {
         menu.getItem(1/*R.id.nav_settings*/).setVisible(false);
     }
     return super.onPrepareOptionsMenu(menu);
 }
Mohd Qasim
  • 896
  • 9
  • 20