0% found this document useful (0 votes)
1K views

Android - How Do I Hide A Menu Item in The Actionbar - Stack Overflow

To hide or show a menu item in the Android action bar: 1. Set a flag to track the visibility state 2. Call invalidateOptionsMenu() to refresh the menu 3. In onCreateOptionsMenu(), check the flag and use findItem() and setVisible() to hide/show the item.

Uploaded by

Hawaz Beyene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Android - How Do I Hide A Menu Item in The Actionbar - Stack Overflow

To hide or show a menu item in the Android action bar: 1. Set a flag to track the visibility state 2. Call invalidateOptionsMenu() to refresh the menu 3. In onCreateOptionsMenu(), check the flag and use findItem() and setVisible() to hide/show the item.

Uploaded by

Hawaz Beyene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

8/8/2020 android - How do I hide a menu item in the actionbar?

- Stack Overflow

How do I hide a menu item in the actionbar?


Asked 8 years, 2 months ago Active 1 month ago Viewed 327k times

I have an action bar with a menuitem. How can I hide/show that menu item?

This is what I'm trying to do:


351
MenuItem item = (MenuItem) findViewById(R.id.addAction);
item.setVisible(false);
this.invalidateOptionsMenu();

74
android android-actionbar menuitem android-menu

edited Feb 27 '15 at 4:41 asked May 21 '12 at 21:18


user4532337 Stir Zoltán
3,801 3 13 13

24 Answers Active Oldest Votes

Get a MenuItem pointing to such item, call setVisible on it to adjust its visibility and then call
invalidateOptionsMenu() on your activity so the ActionBar menu is adjusted accordingly.

487 Update: A MenuItem is not a regular view that's part of your layout. Its something special,
completely different. Your code returns null for item and that's causing the crash. What you
need instead is to do:

MenuItem item = menu.findItem(R.id.addAction);

Here is the sequence in which you should call: first call invalidateOptionsMenu() and then inside
onCreateOptionsMenu(Menu) obtain a reference to the MenuItem (by calling menu.findItem() ) and
call setVisible() on it

edited Nov 3 '18 at 21:29 answered May 21 '12 at 21:24


Jack Bashford K-ballo
36.9k 9 34 57 74.4k 19 139 161

This is what I've thought of initially, but this crashes the application. – Stir Zoltán May 21 '12 at 21:30

3 @Stir Zoltán: Well that's how you do it, you may be doing it wrong if it crashes the application. For
instance, your MenuItem may be null because you are using getItem instead of findItem . We could
never know, without seeing both your code and crash log. – K-ballo May 21 '12 at 21:31

OK but how can I get a reference to the menu outside the onCreateOptionsMenu method? – Stir Zoltán
May 21 '12 at 21:41
By using7our @Stir
site, you acknowledge
Zoltán: thatmake
I believe you you have read with
no sense and that
understand our Cookie
last comment... Policy
Simply get, aPrivacy Policy
reference , andmenu
to your
item at
our Terms of Service. onCreateOptionsMenu , and set the item visibility at that point. Or keep the reference around until

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 1/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow
you decide whether it should be visible or not. – K-ballo May 21 '12 at 21:46

34 I think that (at best) this answer is poorly worded because the sequence of steps is wrong. The correct
sequence is to first call invalidateOptionsMenu() and then inside onCreateOptionsMenu(Menu) obtain
a reference to the MenuItem (by calling menu.findItem() ) and call setVisible() on it. The answers
by suhas_sm and by P1r4nh4 present the correct approach. – Ted Hopp Jun 24 '14 at 17:33

Found an addendum to this question:

If you want to change the visibility of your menu items on the go you just need to set a member
165 variable in your activity to remember that you want to hide the menu and call
invalidateOptionsMenu() and hide the items in your overridden onCreateOptionsMenu(...) method.

//anywhere in your code


...
mState = HIDE_MENU; // setting state
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// inflate menu from xml
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.settings, menu);

if (mState == HIDE_MENU)
{
for (int i = 0; i < menu.size(); i++)
menu.getItem(i).setVisible(false);
}
}

In my example I've hidden all items.

answered Nov 27 '12 at 12:40


P1r4nh4
1,807 1 10 9

2 This is exactly what I've been looking for. holding onto the MenuItem and calling setVisible() didn't seem to
do what I expected (maybe because I was calling invalidateOptionsMenu() after, thus rebuilding the menu),
but either way -- this works exactly as I was hoping, thanks! – Matt Dec 16 '13 at 20:28

4 If you're going to hide every item, there's an easier way. Instead of looping through each item here, you
could just do: if (HIDE_MENU) { return false; } else {
getSupportMenuInflater().inflate(R.menu.menu_settings, menu); return true; } The docs state:
"You must return true for the menu to be displayed; if you return false it will not be shown". – w3bshark Oct
5 '16 at 15:13

Also, I'd like to mention that this approach is much cleaner than the accepted answer. Thanks for sharing! –
w3bshark Oct 5 '16 at 15:14

by calling invalidateOptionMenu() onCreateOptionMenu() called and I handle my conditions in


onCreateOptionMenu(). Thanks for good answer – user1912026 Apr 24 at 20:08
By using our site, you acknowledge thatshould
onPrepareOptionsMenu() you have read for
be used andshow/hide
understand ourinstead
logic CookieofPolicy, Privacy Policy, and as it is
onCreateOptionsMenu()
our Terms of only
Service . once so not useful if you want to change the menu items after its initialization. So inflate in
called

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 2/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow
onCreate, but show/hide in onPrepare. – JCutting8 May 6 at 12:03

Yes.

1. You can set a flag/condition.


91
2. Call invalidateOptionsMenu() when you want to hide the option. This will call
onCreateOptionsMenu() .

3. In onCreateOptionsMenu() , check for the flag/condition and show or hide it the following way:

MenuItem item = menu.findItem(R.id.menu_Done);

if (flag/condition)) {
item.setVisible(false);
} else { }

edited Jun 25 '15 at 13:37 answered Oct 27 '12 at 10:12


ban-geoengineering suhas_sm
14.2k 16 123 206 1,977 16 22

2 Shouldn't it be invalidateOptionsMenu instead of invalidateOptions ? – razz May 4 '15 at 17:42

3 If you make your flag a boolean you can just set the item's visibility to the flag (or !flag) if need be. So,
item.setVisbility(!flag); makes this a one liner with no if condition. – jwehrle May 31 '16 at 0:20

where is the variable "menu" defined? – Jonathan Feb 7 at 9:54

It's passed as a parameter to onCreateOptionsMenu – suhas_sm Feb 10 at 4:20

using this method will mean the item still consumes space and you may end up with a "gap" in your UI
where the button should be. Using removeItem in onPrepareOptionsMenu() will do the trick. – JCutting8
May 6 at 12:05

You can call this:

39 MenuItem item = menu.findItem(R.id.my_item);


item.setVisible(false);

Update:

Make sure your code doesn't returns null for item or it may crash the application.

edited Jul 31 '15 at 19:36 answered May 21 '12 at 21:24


elias
12k 3 33 61

using this method will mean the item still consumes space and you may end up with a "gap" in your UI
By using our where
site, you
theacknowledge
button shouldthat you have
be. Using read andinunderstand
removeItem our Cookie Policy
onPrepareOptionsMenu() will, do
Privacy Policy
the trick. , and
– JCutting8
our Terms of May
Service
6 at. 12:05

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 3/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

I was looking for an answer with a little more context. Now that I have figured it out, I will add that
answer.
33
Hide button by default in menu xml

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

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- hide share button by default -->


<item
android:id="@+id/menu_action_share"
android:icon="@drawable/ic_share_white_24dp"
android:visible="false"
android:title="Share"
app:showAsAction="always"/>

<item
android:id="@+id/menu_action_settings"
android:icon="@drawable/ic_settings_white_24dp"
android:title="Setting"
app:showAsAction="ifRoom"/>

</menu>

Show button in code

But the share button can optionally be shown based on some condition.

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 4/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {


MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem shareItem = menu.findItem(R.id.menu_action_share);

// show the button when some condition is true


if (someCondition) {
shareItem.setVisible(true);
}

return true;
}

See also

Setting Up the App Bar (Android docs for help getting the app/action bar set up)

edited Jan 1 '17 at 10:05 answered Sep 5 '16 at 6:27


Suragch
310k 196 1030
1060

onPrepareOptionsMenu() should be used for show/hide logic instead of onCreateOptionsMenu() as it is


only called once so not useful if you want to change the menu items after its initialization. So inflate in
onCreate, but show/hide in onPrepare, then use invalidateOptionsMenu() to refresh the menu. –
JCutting8 May 6 at 12:07

didn't work for me. I had to explicitly use onPrepareOptionsMenu to set an item invisible.

So use onCreateOptionsMenu to create the menu and onPrepareOptionsMenu to change visibility etc.
24
answered Sep 8 '14 at 22:08
hotzen
2,550 22 36

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
1 For me it worked both ways, but based on Android's docs onPrepareOptionsMenu seems like the right
our Terms of place
Service
to .do this type of operation: "Prepare the Screen's standard options menu to be displayed. This is

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 5/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

called right before the menu is shown, every time it is shown. You can use this method to efficiently
enable/disable items or otherwise dynamically modify the contents." – yuval Sep 15 '15 at 19:19

Yes, this is ideal. onPrepareOptionsMenu() should be used for show/hide logic instead of
onCreateOptionsMenu() as it is only called once so not useful if you want to change the menu items after
its initialization. So inflate in onCreate, but show/hide in onPrepare, then use invalidateOptionsMenu()
to refresh the menu. – JCutting8 May 6 at 12:07

Initially set the menu item visibility to false in the menu layout file as follows :

9 <?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:visible="false"
android:id="@+id/action_do_something"
android:title="@string/txt_do_something"
app:showAsAction="always|withText"
android:icon="@drawable/ic_done"/>
</menu>

You can then simply set the visibility of the menu item to false in your onCreateOptionsMenu()
after inflating the menu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(menu,R.menu.menu);
MenuItem item = menu.findItem(R.id.menuItemId);
if (item != null){
item.setVisible(false);
}
}

edited Nov 13 '17 at 21:38 answered Nov 14 '13 at 10:27


Joseph Akhila
4,705 1 25 34 121 1 8

onCreateOptionsMenu does not return a boolean value. The solution worked perfectly though. – Joseph
Nov 13 '17 at 21:32

Try this:

8 MenuItem myitem = menu.findItem(R.id.my_item);


myitem.setVisible(false);

answered Feb 16 '19 at 10:48


mohamad sheikhi
294 3 13
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 6/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

This worked for me from both Activity and Fragment

7 @Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (menu.findItem(R.id.action_messages) != null)
menu.findItem(R.id.action_messages).setVisible(false);
}

answered Jul 15 '15 at 11:15


Bala Vishnu
2,312 19 19

1 In my case onPrepareOptionsMenu returns boolean, not void. – CoolMind Feb 3 '16 at 8:57

P1r4nh4 answer works fine, I just simplified it using a boolean flag:

6 public int mState = 0; //at the top of the code


//where you want to trigger the hide action
mState = 1; // to hide or mState = 0; to show
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// inflate menu from xml
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.settings, menu);

if (mState == 1) //1 is true, 0 is false


{
//hide only option 2
menu.getItem(2).setVisible(false);
}
}

answered Apr 13 '13 at 19:11


Hiram
2,352 1 13 13

2 Using 0 and 1 is not a "boolean flag", it's pseudo-boolean. besides, there is no reason to use an actual
boolean here. – Ingo Bürk Jul 27 '13 at 22:24

2 Also, you could have several states, not just one to hide and one to show. Depending on the complexity of
your app and your refusal to write new activities for new app states you might have a SHOW_ALL state, a
HIDE_ALL state, but also a HIDE_EDIT or HIDE_SHARE state in case you want to hide certain parts of
your menu. – P1r4nh4 Dec 11 '13 at 9:43

According to Android Developer Official site,OnCreateOptionMenu(Menu menu) is not


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
recomended for changing menu items or icons, visibility..etc at Runtime.
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 7/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

4 After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you
populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for
some reason. However, you should use onCreateOptionsMenu() only to create the initial
menu state and not to make changes during the activity lifecycle.

If you want to modify the options menu based on events that occur during the activity
lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the
Menu object as it currently exists so you can modify it, such as add, remove, or disable items.
(Fragments also provide an onPrepareOptionsMenu() callback.) --AndroidDeveloper Official
Site --

As Recomended You can use this onOptionsItemSelected(MenuItem item) method track user
inputs.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.edit) {
Intent intent = new Intent(this, ExampleActivity.class);
intent.putExtra(BUNDLE_KEY, mConnection);
startActivityForResult(intent, PICK_CHANGE_REQUEST);
return true;
} else if (id == R.id.delete) {
showDialog(this);
return true;
}

return super.onOptionsItemSelected(item);
}

If you need to change Menu Items at Run time, You can use onPrepareOptionsMenu(Menu
menu) to change them

@Override
public boolean onPrepareOptionsMenu(Menu menu){

if (Utils.checkNetworkStatus(ExampleActivity.this)) {
menu.findItem(R.id.edit).setVisible(true);
menu.findItem(R.id.delete).setVisible(true);
}else {
menu.findItem(R.id.edit).setVisible(false);
menu.findItem(R.id.delete).setVisible(false);
}
return true;
}

edited Jun 20 at 9:12 answered Sep 30 '17 at 15:55


Community ♦ Chanaka Fernando
1 1 1,402 11 18

some items are always visible, so when I setVisible(false) for the always visible item, it does not
By using our disappear
site, you acknowledge
until I click onthat you have
the three dotsread and
(Menu understand
itself). If I use our Cookie Policy, Privacy Policy
invalidateOptionsMenu() in , and
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 8/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

onPrepareOptionsMenu the items immediately reorganize itself, but they loose their actions (if I click on
any item, it does nothing). – Aliton Oliveira Feb 18 '19 at 21:40

set a value to a variable and call invalidateOptionsMenu();

for example
3
selectedid=arg2;
invalidateOptionsMenu();

public boolean onPrepareOptionsMenu(Menu menu) {

if(selectedid==1){
menu.findItem(R.id.action_setting).setVisible(false);
menu.findItem(R.id.action_s2).setVisible(false);
menu.findItem(R.id.action_s3).setVisible(false);
}
else{
if(selectedid==2){
menu.findItem(R.id.action_search).setVisible(false);
menu.findItem(R.id.action_s4).setVisible(false);
menu.findItem(R.id.action_s5).setVisible(false);
}
}
return super.onPrepareOptionsMenu(menu);
}

answered Nov 20 '14 at 11:52


zacharia
1,003 8 22

some items are always visible, so when I setVisible(false) for the always visible item, it does not
disappear until I click on the three dots (Menu itself). If I use invalidateOptionsMenu() in
onPrepareOptionsMenu the items immediately reorganize itself, but they loose their actions (if I click on
any item, it does nothing). – Aliton Oliveira Feb 18 '19 at 21:42

You can use toolbar.getMenu().clear(); to hide all the menu items at once

answered Nov 24 '16 at 5:47


3
Ajeet Yadav
637 1 9 27

@Ajeet_Yadav is there a way to make the menu visible after you clear it? – Shawn Nov 24 '19 at 8:16

By setting the Visibility of all items in Menu, the appbar menu or overflow menu will be Hide
By usingautomatically
our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.
3
https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 9/15
8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

Example

private Menu menu_change_language;

...

...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(true);

return super.onCreateOptionsMenu(menu);
}

Before going to other fragment use bellow code:

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

answered Mar 26 '18 at 8:10


Hantash Nadeem
306 3 7

https://stackoverflow.com/a/21215280/466363 - answered by Look Alterno and Sufian

ActivityCompat.invalidateOptionsMenu() doesn't callback onPrepareOptionsMenu(); it just


2 update the menu directly.
My someMethod() get called from several places, even before onCreateOptionsMenu(), so I
must check mMenu != null.
should work using API 8

private Menu mMenu;


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.track_fragment, menu);
mMenu = menu;
}
...
private void someMethod() {
...
if (mMenu != null) {
By using our site, youMenuItem item = mMenu.findItem(R.id.new_track);
acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
if (item != null) {
our Terms of Service. item.setVisible(false);

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same gro… 10/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow
ActivityCompat.invalidateOptionsMenu(this.getActivity());
}
}
...
}

ActivityCompat.invalidateOptionsMenu() doesn't callback onPrepareOptionsMenu(); it just


update the menu directly.
My someMethod() get called from several places, even before onCreateOptionsMenu(), so I
must check mMenu != null.
should work using API 8

edited May 23 '17 at 11:47 answered Jan 25 '15 at 12:35


Community ♦ Shimon Doodkin
1 1 3,318 27 31

If you did everything as in above answers, but a menu item is still visible, check that you
reference to the unique resource. For instance, in onCreateOptionsMenu or
2 onPrepareOptionsMenu

@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuOpen = menu.findItem(R.id.menu_open);
menuOpen.setVisible(false);
}

Ctrl+Click R.id.menu_open and check that it exists only in one menu file. In case when this
resource is already used anywhere and loaded in an activity, it will try to hide there.

answered Mar 3 '17 at 18:22


CoolMind
11.6k 6 99 125

The best way to hide all items in a menu with just one command is to use "group" on your menu
xml. Just add all menu items that will be in your overflow menu inside the same group.
2 In this example we have two menu items that will always show (regular item and search) and
three overflow items:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/someItemNotToHide1"
android:title="ITEM"
app:showAsAction="always" />

<item
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
android:id="@+id/someItemNotToHide2"
android:icon="@android:drawable/ic_menu_search"
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same grou… 11/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>

<group android:id="@+id/overFlowItemsToHide">
<item android:id="@+id/someID"
android:orderInCategory="1" app:showAsAction="never" />
<item android:id="@+id/someID2"
android:orderInCategory="1" app:showAsAction="never" />
<item android:id="@+id/someID3"
android:orderInCategory="1" app:showAsAction="never" />
</group>
</menu>

Then, on your activity (preferable at onCreateOptionsMenu), use command setGroupVisible to


set all menu items visibility to false or true.

public boolean onCreateOptionsMenu(Menu menu) {


menu.setGroupVisible(R.id.overFlowItems, false); // Or true to be visible
}

If you want to use this command anywhere else on your activity, be sure to save menu class to
local, and always check if menu is null, because you can execute before createOptionsMenu:

Menu menu;

public boolean onCreateOptionsMenu(Menu menu) {


this.menu = menu;

public void hideMenus() {


if (menu != null) menu.setGroupVisible(R.id.overFlowItems, false); // Or true to
be visible
}

answered Jun 11 '17 at 23:01


Adriano Moutinho
413 3 7

For those using the Appcompat library: If your Activity subclasses ActionBarActivity, you can call
supportInvalidateOptionsMenu()
1 Seen here: https://stackoverflow.com/a/19649877/1562524

edited May 23 '17 at 12:34 answered Apr 3 '15 at 5:29


Community ♦ RuideraJ
1 1 71 2 7

I think a better approach would be to use a member variable for the menu, initialize it in
onCreateOptionsMenu() and just use setVisible() afterwards, without invalidating the options
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
1 menu.
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same gro… 12/15


8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow
answered Apr 11 '17 at 10:39
Catalin Clabescu
69 3

this code worked for me

1 @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
if (Application.sharedPreferences.getInt("type",1) == 2)
{
menuItem = menu.findItem(R.id.menu_travel_orders);
menuItem.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}

answered Nov 30 '17 at 16:37


Sai Gopi Me
7,842 54 37

This Approach worked for me:

1 private Menu thismenu;

if (condition)
{
if(thismenu != null)
{
thismenu.findItem(R.id.menu_save).setVisible(true);
Toast.makeText(ProfileActivity.this,
""+thismenu.findItem(R.id.menu_save).getTitle(),
Toast.LENGTH_SHORT).show();
}else
{
thismenu.findItem(R.id.menu_save).setVisible(false);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu, menu);
thismenu = menu;

return true;
}

edited Jul 22 '18 at 1:13 answered Jul 22 '18 at 0:55


Stephen Rauch Forest baba
37.2k 15 57 83 109 4

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
You are trying to access a menu item from an activity, which dont have the access to scope. The
our Terms of Service.
call to find the menu item will return null, because the view is not binded with neither the activity
https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same gro… 13/15
8/8/2020 android - How do I hide a menu item in the actionbar? - Stack Overflow

1 nor the layout you are calling from.

The menu items are binded with items such as "Navigation Bar" which in turn are binded with
the corresponding activity.

So initialize those views in activity (), and then access the menu items withing that views.

Navigation navView;
navView = findViewById(R.id.navigationView);

MenuItem item = navView.getMenu().findItem(R.id.item_hosting);


item.setVisible(false);

answered Mar 23 '19 at 7:05


VeeyaaR
91 2 6

use invalidateOptionsMenu()

in order to call onPrepareOptionsMenu(menu: Menu?)


1
you should use onCreateOptionsMenu() only to create the initial menu state and not to make
changes during the activity lifecycle...

When an event occurs and you want to perform a menu update, you must call
invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

https://developer.android.com/guide/topics/ui/menus

answered Jun 30 '19 at 8:38


Dan Alboteanu
4,880 31 30

Android kotlin, hide or set visibility of a menu item in the action bar programmatically.

1
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.main_menu, menu)
val method = menu.findItem(R.id.menu_method)
method.isVisible = false //if want to show set true
return super.onCreateOptionsMenu(menu)
}

answered Jun 12 at 21:49


Aftab Alam
633 4 8
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar#:~:text=The best way to hide,menu inside the same gro… 14/15

You might also like