skip to Main Content

I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I’m trying to call it like this:

val menu = findViewById<MenuItem>(R.id.deleteBarra)

2

Answers


  1. Under what condition you want it to be hidden, you can add it under that condition.

      menu.visibility = View.INVISIBLE
    

    or

       menu.visibility = View.GONE
    
    Login or Signup to reply.
  2. Here’s the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. onPrepareOptionsMenu gets called every time the menu needs to be displayed (instead of once, during setup).

    So you can set a boolean or whatever to say whether the item should be shown, and call invalidateOptionsMenu() to redisplay it. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search