Set visibility in Menu programmatically android
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
add a comment |
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
add a comment |
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
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
android
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
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
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;
+1 for nice answer...
– Lucifer
Jan 27 '12 at 7:54
2
Two tips: 1.you can tighten that up by writingregister.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 ainvalidateOptionsMenu();
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
|
show 2 more comments
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;
}
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
add a comment |
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
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 ifBuild.VERSION.SDK_INT >= 11
– Sergey Benner
Apr 12 '13 at 19:45
add a comment |
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);
}
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
add a comment |
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;
}
add a comment |
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
+1 for nice answer...
– Lucifer
Jan 27 '12 at 7:54
2
Two tips: 1.you can tighten that up by writingregister.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 ainvalidateOptionsMenu();
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
|
show 2 more comments
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;
+1 for nice answer...
– Lucifer
Jan 27 '12 at 7:54
2
Two tips: 1.you can tighten that up by writingregister.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 ainvalidateOptionsMenu();
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
|
show 2 more comments
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;
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;
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 writingregister.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 ainvalidateOptionsMenu();
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
|
show 2 more comments
+1 for nice answer...
– Lucifer
Jan 27 '12 at 7:54
2
Two tips: 1.you can tighten that up by writingregister.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 ainvalidateOptionsMenu();
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
|
show 2 more comments
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;
}
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
add a comment |
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;
}
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
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
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
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 ifBuild.VERSION.SDK_INT >= 11
– Sergey Benner
Apr 12 '13 at 19:45
add a comment |
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
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 ifBuild.VERSION.SDK_INT >= 11
– Sergey Benner
Apr 12 '13 at 19:45
add a comment |
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
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
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 ifBuild.VERSION.SDK_INT >= 11
– Sergey Benner
Apr 12 '13 at 19:45
add a comment |
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 ifBuild.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
add a comment |
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);
}
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
add a comment |
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);
}
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
add a comment |
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);
}
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);
}
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 18 '18 at 15:17
guyguy
829216
829216
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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