Set visibility in Menu programmatically android












46















So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:



public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}


But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.










share|improve this question





























    46















    So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:



    public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
    }

    public boolean onOptionsItemSelected (MenuItem item){
    switch (item.getItemId()){
    case R.id.menuregistrar:
    break;
    case R.id.menusalir:
    break;
    }
    return true;
    }


    But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.










    share|improve this question



























      46












      46








      46


      13






      So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:



      public boolean onCreateOptionsMenu(Menu menu){
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
      }

      public boolean onOptionsItemSelected (MenuItem item){
      switch (item.getItemId()){
      case R.id.menuregistrar:
      break;
      case R.id.menusalir:
      break;
      }
      return true;
      }


      But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.










      share|improve this question
















      So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:



      public boolean onCreateOptionsMenu(Menu menu){
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
      }

      public boolean onOptionsItemSelected (MenuItem item){
      switch (item.getItemId()){
      case R.id.menuregistrar:
      break;
      case R.id.menusalir:
      break;
      }
      return true;
      }


      But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.







      android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 '17 at 16:39









      Cœur

      19.1k9114155




      19.1k9114155










      asked Jan 27 '12 at 7:39









      zapoteczapotec

      1,62532342




      1,62532342
























          6 Answers
          6






          active

          oldest

          votes


















          128














          Put this method in your Activity



          public boolean onPrepareOptionsMenu(Menu menu)
          {
          MenuItem register = menu.findItem(R.id.menuregistrar);
          if(userRegistered)
          {
          register.setVisible(false);
          }
          else
          {
          register.setVisible(true);
          }
          return true;
          }


          in shorter version you could write:



          MenuItem register = menu.findItem(R.id.menuregistrar);      
          register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
          return true;





          share|improve this answer


























          • +1 for nice answer...

            – Lucifer
            Jan 27 '12 at 7:54






          • 2





            Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

            – W.K.S
            Jun 26 '13 at 12:27






          • 1





            @W.K.S agreed. thanks for the comment.

            – Adil Soomro
            Jun 26 '13 at 12:35






          • 2





            You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

            – rundavidrun
            Nov 1 '14 at 4:06













          • it's good job. Thank you very much

            – Abror Esonaliyev
            May 7 '17 at 13:31



















          8














          I would simplify Adil's solution even further with the following:



          public boolean onPrepareOptionsMenu(Menu menu)
          {
          MenuItem registrar = menu.findItem(R.id.menuregistrar);
          registrar.setVisible(!isRegistered);
          return true;
          }





          share|improve this answer



















          • 1





            Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

            – Adil Soomro
            Dec 13 '12 at 6:10








          • 1





            @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

            – Stan
            Feb 28 '13 at 10:25



















          3














          Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.



          Cheers






          share|improve this answer



















          • 2





            onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

            – javahead76
            Apr 12 '13 at 19:19











          • @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

            – Sergey Benner
            Apr 12 '13 at 19:45



















          2














          Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//



          Example



          private Menu menu_change_language;
          ...

          ...

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
          ...
          ...
          menu_change_language = menu;
          ...
          ...

          return super.onCreateOptionsMenu(menu);
          }


          use code below for hiding Menu Item:



          if(menu_change_language != null){                 
          menu_change_language.findItem(R.id.menu_change_language)
          .setVisible(false);
          }





          share|improve this answer
























          • this is useful if you want to visible menu after some certain operation. Thanks.

            – Arslan Maqbool
            Sep 25 '18 at 16:27











          • Please vote my answer if its useful @ArslanMaqbool

            – Hantash Nadeem
            Sep 28 '18 at 12:11





















          1














          If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu



          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
          item.setVisible(true);
          return true;
          }


          OR
          for item in the menu that you didn't click on



          private Menu globalMenuItem;
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu."menu Xml Name", menu);
          globalMenuItem = menu;
          return true;
          }
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
          globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
          return true;
          }





          share|improve this answer































            1














            Simply do one thing get the id of the item of menu from this line:



            Menu menu =navigationView.getMenu();

            MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);


            and than make it visible it accourding to you by this line:



            nav_dashboard.setVisible(true/false);





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9030268%2fset-visibility-in-menu-programmatically-android%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              128














              Put this method in your Activity



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem register = menu.findItem(R.id.menuregistrar);
              if(userRegistered)
              {
              register.setVisible(false);
              }
              else
              {
              register.setVisible(true);
              }
              return true;
              }


              in shorter version you could write:



              MenuItem register = menu.findItem(R.id.menuregistrar);      
              register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
              return true;





              share|improve this answer


























              • +1 for nice answer...

                – Lucifer
                Jan 27 '12 at 7:54






              • 2





                Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

                – W.K.S
                Jun 26 '13 at 12:27






              • 1





                @W.K.S agreed. thanks for the comment.

                – Adil Soomro
                Jun 26 '13 at 12:35






              • 2





                You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

                – rundavidrun
                Nov 1 '14 at 4:06













              • it's good job. Thank you very much

                – Abror Esonaliyev
                May 7 '17 at 13:31
















              128














              Put this method in your Activity



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem register = menu.findItem(R.id.menuregistrar);
              if(userRegistered)
              {
              register.setVisible(false);
              }
              else
              {
              register.setVisible(true);
              }
              return true;
              }


              in shorter version you could write:



              MenuItem register = menu.findItem(R.id.menuregistrar);      
              register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
              return true;





              share|improve this answer


























              • +1 for nice answer...

                – Lucifer
                Jan 27 '12 at 7:54






              • 2





                Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

                – W.K.S
                Jun 26 '13 at 12:27






              • 1





                @W.K.S agreed. thanks for the comment.

                – Adil Soomro
                Jun 26 '13 at 12:35






              • 2





                You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

                – rundavidrun
                Nov 1 '14 at 4:06













              • it's good job. Thank you very much

                – Abror Esonaliyev
                May 7 '17 at 13:31














              128












              128








              128







              Put this method in your Activity



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem register = menu.findItem(R.id.menuregistrar);
              if(userRegistered)
              {
              register.setVisible(false);
              }
              else
              {
              register.setVisible(true);
              }
              return true;
              }


              in shorter version you could write:



              MenuItem register = menu.findItem(R.id.menuregistrar);      
              register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
              return true;





              share|improve this answer















              Put this method in your Activity



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem register = menu.findItem(R.id.menuregistrar);
              if(userRegistered)
              {
              register.setVisible(false);
              }
              else
              {
              register.setVisible(true);
              }
              return true;
              }


              in shorter version you could write:



              MenuItem register = menu.findItem(R.id.menuregistrar);      
              register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
              return true;






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 12 '14 at 14:10

























              answered Jan 27 '12 at 7:44









              Adil SoomroAdil Soomro

              33.7k892134




              33.7k892134













              • +1 for nice answer...

                – Lucifer
                Jan 27 '12 at 7:54






              • 2





                Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

                – W.K.S
                Jun 26 '13 at 12:27






              • 1





                @W.K.S agreed. thanks for the comment.

                – Adil Soomro
                Jun 26 '13 at 12:35






              • 2





                You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

                – rundavidrun
                Nov 1 '14 at 4:06













              • it's good job. Thank you very much

                – Abror Esonaliyev
                May 7 '17 at 13:31



















              • +1 for nice answer...

                – Lucifer
                Jan 27 '12 at 7:54






              • 2





                Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

                – W.K.S
                Jun 26 '13 at 12:27






              • 1





                @W.K.S agreed. thanks for the comment.

                – Adil Soomro
                Jun 26 '13 at 12:35






              • 2





                You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

                – rundavidrun
                Nov 1 '14 at 4:06













              • it's good job. Thank you very much

                – Abror Esonaliyev
                May 7 '17 at 13:31

















              +1 for nice answer...

              – Lucifer
              Jan 27 '12 at 7:54





              +1 for nice answer...

              – Lucifer
              Jan 27 '12 at 7:54




              2




              2





              Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

              – W.K.S
              Jun 26 '13 at 12:27





              Two tips: 1.you can tighten that up by writing register.setVisible(userNotRegistered); 2. Boolean variables are usually positive i.e. if(!userRegistered). if(!UsersNotRegistered) is difficult to understand: if the user is NOt not-registered?

              – W.K.S
              Jun 26 '13 at 12:27




              1




              1





              @W.K.S agreed. thanks for the comment.

              – Adil Soomro
              Jun 26 '13 at 12:35





              @W.K.S agreed. thanks for the comment.

              – Adil Soomro
              Jun 26 '13 at 12:35




              2




              2





              You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

              – rundavidrun
              Nov 1 '14 at 4:06







              You might also want to do a invalidateOptionsMenu(); to refresh the menu if the user already has it open.

              – rundavidrun
              Nov 1 '14 at 4:06















              it's good job. Thank you very much

              – Abror Esonaliyev
              May 7 '17 at 13:31





              it's good job. Thank you very much

              – Abror Esonaliyev
              May 7 '17 at 13:31













              8














              I would simplify Adil's solution even further with the following:



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem registrar = menu.findItem(R.id.menuregistrar);
              registrar.setVisible(!isRegistered);
              return true;
              }





              share|improve this answer



















              • 1





                Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

                – Adil Soomro
                Dec 13 '12 at 6:10








              • 1





                @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

                – Stan
                Feb 28 '13 at 10:25
















              8














              I would simplify Adil's solution even further with the following:



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem registrar = menu.findItem(R.id.menuregistrar);
              registrar.setVisible(!isRegistered);
              return true;
              }





              share|improve this answer



















              • 1





                Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

                – Adil Soomro
                Dec 13 '12 at 6:10








              • 1





                @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

                – Stan
                Feb 28 '13 at 10:25














              8












              8








              8







              I would simplify Adil's solution even further with the following:



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem registrar = menu.findItem(R.id.menuregistrar);
              registrar.setVisible(!isRegistered);
              return true;
              }





              share|improve this answer













              I would simplify Adil's solution even further with the following:



              public boolean onPrepareOptionsMenu(Menu menu)
              {
              MenuItem registrar = menu.findItem(R.id.menuregistrar);
              registrar.setVisible(!isRegistered);
              return true;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 12 '12 at 19:40









              jakeneffjakeneff

              36846




              36846








              • 1





                Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

                – Adil Soomro
                Dec 13 '12 at 6:10








              • 1





                @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

                – Stan
                Feb 28 '13 at 10:25














              • 1





                Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

                – Adil Soomro
                Dec 13 '12 at 6:10








              • 1





                @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

                – Stan
                Feb 28 '13 at 10:25








              1




              1





              Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

              – Adil Soomro
              Dec 13 '12 at 6:10







              Its good, but some new users wouldn't get it, as they are in hurry and want to get there problem solved asap, so I expended it to show what is actually happening there. :)

              – Adil Soomro
              Dec 13 '12 at 6:10






              1




              1





              @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

              – Stan
              Feb 28 '13 at 10:25





              @AdilSoomro, this code is simplier (and more convenient if "they are in hurry") than in your answer, so I'd suggest to edit the original answer.

              – Stan
              Feb 28 '13 at 10:25











              3














              Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.



              Cheers






              share|improve this answer



















              • 2





                onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

                – javahead76
                Apr 12 '13 at 19:19











              • @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

                – Sergey Benner
                Apr 12 '13 at 19:45
















              3














              Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.



              Cheers






              share|improve this answer



















              • 2





                onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

                – javahead76
                Apr 12 '13 at 19:19











              • @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

                – Sergey Benner
                Apr 12 '13 at 19:45














              3












              3








              3







              Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.



              Cheers






              share|improve this answer













              Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.



              Cheers







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 27 '12 at 7:45









              Sergey BennerSergey Benner

              4,08921928




              4,08921928








              • 2





                onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

                – javahead76
                Apr 12 '13 at 19:19











              • @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

                – Sergey Benner
                Apr 12 '13 at 19:45














              • 2





                onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

                – javahead76
                Apr 12 '13 at 19:19











              • @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

                – Sergey Benner
                Apr 12 '13 at 19:45








              2




              2





              onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

              – javahead76
              Apr 12 '13 at 19:19





              onPrepareOptionsMenu is also called any time you call invalidateOptionsMenu, so it may be called more than once.

              – javahead76
              Apr 12 '13 at 19:19













              @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

              – Sergey Benner
              Apr 12 '13 at 19:45





              @javahead76 this call will only work if Build.VERSION.SDK_INT >= 11

              – Sergey Benner
              Apr 12 '13 at 19:45











              2














              Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//



              Example



              private Menu menu_change_language;
              ...

              ...

              @Override
              public boolean onCreateOptionsMenu(Menu menu) {
              ...
              ...
              menu_change_language = menu;
              ...
              ...

              return super.onCreateOptionsMenu(menu);
              }


              use code below for hiding Menu Item:



              if(menu_change_language != null){                 
              menu_change_language.findItem(R.id.menu_change_language)
              .setVisible(false);
              }





              share|improve this answer
























              • this is useful if you want to visible menu after some certain operation. Thanks.

                – Arslan Maqbool
                Sep 25 '18 at 16:27











              • Please vote my answer if its useful @ArslanMaqbool

                – Hantash Nadeem
                Sep 28 '18 at 12:11


















              2














              Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//



              Example



              private Menu menu_change_language;
              ...

              ...

              @Override
              public boolean onCreateOptionsMenu(Menu menu) {
              ...
              ...
              menu_change_language = menu;
              ...
              ...

              return super.onCreateOptionsMenu(menu);
              }


              use code below for hiding Menu Item:



              if(menu_change_language != null){                 
              menu_change_language.findItem(R.id.menu_change_language)
              .setVisible(false);
              }





              share|improve this answer
























              • this is useful if you want to visible menu after some certain operation. Thanks.

                – Arslan Maqbool
                Sep 25 '18 at 16:27











              • Please vote my answer if its useful @ArslanMaqbool

                – Hantash Nadeem
                Sep 28 '18 at 12:11
















              2












              2








              2







              Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//



              Example



              private Menu menu_change_language;
              ...

              ...

              @Override
              public boolean onCreateOptionsMenu(Menu menu) {
              ...
              ...
              menu_change_language = menu;
              ...
              ...

              return super.onCreateOptionsMenu(menu);
              }


              use code below for hiding Menu Item:



              if(menu_change_language != null){                 
              menu_change_language.findItem(R.id.menu_change_language)
              .setVisible(false);
              }





              share|improve this answer













              Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//



              Example



              private Menu menu_change_language;
              ...

              ...

              @Override
              public boolean onCreateOptionsMenu(Menu menu) {
              ...
              ...
              menu_change_language = menu;
              ...
              ...

              return super.onCreateOptionsMenu(menu);
              }


              use code below for hiding Menu Item:



              if(menu_change_language != null){                 
              menu_change_language.findItem(R.id.menu_change_language)
              .setVisible(false);
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 26 '18 at 8:15









              Hantash NadeemHantash Nadeem

              15115




              15115













              • this is useful if you want to visible menu after some certain operation. Thanks.

                – Arslan Maqbool
                Sep 25 '18 at 16:27











              • Please vote my answer if its useful @ArslanMaqbool

                – Hantash Nadeem
                Sep 28 '18 at 12:11





















              • this is useful if you want to visible menu after some certain operation. Thanks.

                – Arslan Maqbool
                Sep 25 '18 at 16:27











              • Please vote my answer if its useful @ArslanMaqbool

                – Hantash Nadeem
                Sep 28 '18 at 12:11



















              this is useful if you want to visible menu after some certain operation. Thanks.

              – Arslan Maqbool
              Sep 25 '18 at 16:27





              this is useful if you want to visible menu after some certain operation. Thanks.

              – Arslan Maqbool
              Sep 25 '18 at 16:27













              Please vote my answer if its useful @ArslanMaqbool

              – Hantash Nadeem
              Sep 28 '18 at 12:11







              Please vote my answer if its useful @ArslanMaqbool

              – Hantash Nadeem
              Sep 28 '18 at 12:11













              1














              If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu



              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
              item.setVisible(true);
              return true;
              }


              OR
              for item in the menu that you didn't click on



              private Menu globalMenuItem;
              @Override
              public boolean onCreateOptionsMenu(Menu menu) {
              getMenuInflater().inflate(R.menu."menu Xml Name", menu);
              globalMenuItem = menu;
              return true;
              }
              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
              globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
              return true;
              }





              share|improve this answer




























                1














                If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu



                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                item.setVisible(true);
                return true;
                }


                OR
                for item in the menu that you didn't click on



                private Menu globalMenuItem;
                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu."menu Xml Name", menu);
                globalMenuItem = menu;
                return true;
                }
                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
                return true;
                }





                share|improve this answer


























                  1












                  1








                  1







                  If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu



                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                  item.setVisible(true);
                  return true;
                  }


                  OR
                  for item in the menu that you didn't click on



                  private Menu globalMenuItem;
                  @Override
                  public boolean onCreateOptionsMenu(Menu menu) {
                  getMenuInflater().inflate(R.menu."menu Xml Name", menu);
                  globalMenuItem = menu;
                  return true;
                  }
                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                  globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
                  return true;
                  }





                  share|improve this answer













                  If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu



                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                  item.setVisible(true);
                  return true;
                  }


                  OR
                  for item in the menu that you didn't click on



                  private Menu globalMenuItem;
                  @Override
                  public boolean onCreateOptionsMenu(Menu menu) {
                  getMenuInflater().inflate(R.menu."menu Xml Name", menu);
                  globalMenuItem = menu;
                  return true;
                  }
                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                  globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
                  return true;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 15:17









                  guyguy

                  829216




                  829216























                      1














                      Simply do one thing get the id of the item of menu from this line:



                      Menu menu =navigationView.getMenu();

                      MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);


                      and than make it visible it accourding to you by this line:



                      nav_dashboard.setVisible(true/false);





                      share|improve this answer






























                        1














                        Simply do one thing get the id of the item of menu from this line:



                        Menu menu =navigationView.getMenu();

                        MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);


                        and than make it visible it accourding to you by this line:



                        nav_dashboard.setVisible(true/false);





                        share|improve this answer




























                          1












                          1








                          1







                          Simply do one thing get the id of the item of menu from this line:



                          Menu menu =navigationView.getMenu();

                          MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);


                          and than make it visible it accourding to you by this line:



                          nav_dashboard.setVisible(true/false);





                          share|improve this answer















                          Simply do one thing get the id of the item of menu from this line:



                          Menu menu =navigationView.getMenu();

                          MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);


                          and than make it visible it accourding to you by this line:



                          nav_dashboard.setVisible(true/false);






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 22 '18 at 11:32









                          AS Mackay

                          2,01351121




                          2,01351121










                          answered Nov 21 '18 at 12:40









                          Mukesh RawatMukesh Rawat

                          212




                          212






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9030268%2fset-visibility-in-menu-programmatically-android%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)