How do I hide a menu item in the actionbar?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android, hiding a menu item is usually a visibility problem, not a layout problem. You define the item in XML, inflate the menu, and then decide at runtime whether the corresponding MenuItem should be visible.
The lifecycle detail that matters is that menu visibility usually belongs in onPrepareOptionsMenu, not only in onCreateOptionsMenu. That is how you keep the menu synchronized with changing UI state.
Define the Menu Item
Start with an ordinary menu resource:
There is nothing special about the XML for a hideable item. Hiding and showing happens later through the MenuItem API.
Toggle Visibility in an Activity
A standard AppCompatActivity solution looks like this:
The important call is invalidateOptionsMenu(). It tells Android to prepare the menu again, which reruns the visibility logic.
Why onPrepareOptionsMenu Is the Right Place
onCreateOptionsMenu mainly inflates the menu. It may run only once for the activity instance. If the user logs in, changes selection, enters edit mode, or receives new data, that original callback is not enough to keep the menu current.
onPrepareOptionsMenu is designed for the dynamic part. That makes it the right place to hide or show items based on state that can change over time.
If the state truly never changes after creation, setting visibility once is fine. But if the answer can change, call invalidateOptionsMenu() and let the framework re-prepare the menu.
Modern AndroidX Menu Handling
If you are using fragments and a Toolbar, AndroidX MenuProvider is often cleaner than overriding activity methods:
The concept is the same. You still decide visibility at prepare time.
Hide Versus Disable
Sometimes removing the item is not the best user experience. If the action should remain visible but unavailable, disable it instead:
Hiding removes the affordance completely. Disabling communicates that the action exists but is not currently available. Choose the behavior that best fits the product.
Common Pitfalls
One common mistake is setting visibility in onCreateOptionsMenu and expecting the menu to update automatically later. It will not unless the menu is invalidated and prepared again.
Another mistake is holding a stale MenuItem reference across recreations. It is safer to look the item up each time the menu is prepared.
A third issue is treating a hidden item as a security boundary. UI visibility is only presentation. Real permission checks still belong in the action handler or on the server.
Summary
- Define the menu item normally in XML.
- Use
menu.findItem(...).isVisibleto hide or show it. - Put dynamic visibility logic in
onPrepareOptionsMenuoronPrepareMenu. - Call
invalidateOptionsMenu()when the underlying state changes. - Hide an item only when disappearance is the right UX; otherwise consider disabling it.
Related reading
- How do I hide the status bar in a Swift iOS app?
- How do I hide the status bar in a Swift iOS app?
- How do I implement an Objective-C singleton that is compatible with ARC?
- How do I import a Swift file from another Swift file?
- How do I inspect the view hierarchy in iOS?
- How do I launch the Android emulator from the command line?
- How do I load an HTTP URL with App Transport Security enabled in iOS 9?
- How do I locate the CGRect for a substring of text in a UILabel?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.