Android Hide Menu

Apr 21, 2019
class MyQuoteListFragment : Fragment() { // or AppCompatActivity    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setHasOptionsMenu(true) // only required for Fragment    }    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {        inflater.inflate(R.menu.myquote_list, menu)        menu.findItem(R.id.action_sync_google_sheet).also { menuItem ->            if (menuItem != null) {                menuItem.isVisible = true            }        }        super.onCreateOptionsMenu(menu, inflater)    }}

Call activity?.invalidateOptionsMenu() to refresh menu when the conditions changes.

onCreateOptionsMenu

This is only called once, the first time the options menu is displayed.

onPrepareOptionsMenu

This is called right before the menu is shown, every time it is shown.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.