How to change menu item text dynamically in Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Changing a menu item's title at runtime is common when the UI reflects state such as login, edit mode, or unsaved changes. The key is to update the menu during the correct lifecycle phase and trigger a refresh when state changes, rather than setting the title once and assuming Android will update it automatically later.
Understand When Menu Text Is Evaluated
In an Activity, the menu is usually inflated in onCreateOptionsMenu, but dynamic state should usually be applied in onPrepareOptionsMenu. That method can run again after you call invalidateOptionsMenu(), which is what makes runtime updates visible.
If you only change the title during menu creation, later state changes may never show up in the toolbar.
Activity Pattern With invalidateOptionsMenu
This is the classic pattern for an Activity that changes a menu title when auth state changes:
The refresh call is the important part. It tells Android to rebuild the visible menu state using the latest data.
Modern Fragment Pattern With MenuProvider
For fragments, the modern pattern is to use a MenuProvider tied to the fragment view lifecycle.
This keeps menu ownership aligned with the fragment lifecycle and avoids stale references to destroyed views.
Keep Titles in String Resources
Even when the title is dynamic, the actual text should still come from string resources.
That matters for localization, testing, and theme consistency. Hardcoded strings tend to survive much longer in codebases than intended and become an internationalization problem later.
Drive Menu State From Real UI State
In larger apps, menu text often depends on ViewModel state rather than on one local field. The update logic should still stay small and deterministic.
This pattern keeps the menu reactive without scattering title changes across button handlers, callbacks, and lifecycle methods.
Also remember that action items can move into the overflow menu on smaller screens. A dynamic title that is clear in the toolbar should still make sense when shown in overflow.
Common Pitfalls
The most common mistake is updating the underlying state but forgetting to call invalidateOptionsMenu, so the title never refreshes.
Another common issue is setting the title only in onCreateOptionsMenu and expecting later state changes to appear automatically. Developers also often hardcode the text instead of using resources, which creates localization problems, or they mutate menu state from places that are not lifecycle-safe for the current fragment.
Summary
- Use
onPrepareOptionsMenuorMenuProvider.onPrepareMenufor dynamic menu titles. - Call
invalidateOptionsMenu()whenever the state that drives the title changes. - Keep the displayed strings in resources, not inline literals.
- For fragments, prefer
MenuProvidertied to the view lifecycle. - Derive menu text from real app state rather than from scattered ad hoc flags.

