How to make a menu item visible on the toolbar by clicking on an existing one?

2

I have a menu in my toolbar, it has the following actions:

  • R.id.action_editar
  • R.id.action_salvar

How can I make it so that when I click on the edit action, the save action button becomes visible?

Would this be done in the onOptionsItemSelected () method?
Because I can not call my menu on it to set the second action as visible, is there any way to do this?

    
asked by anonymous 01.02.2016 / 17:51

1 answer

2

Yes, this should be done in the onOptionsItemSelected() method, but first you have to get and save a reference to the MenuItem save method in onCreateOptionsMenu() :

//Variável para guardar o MenuItem salvar
MenuItem actionSalvar;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.seu_menu, menu);

    //Guarda a referência
    actionSalvar = menu.findItem(R.id.action_salvar);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    switch (id) {
    case R.id.action_editar:
        ...
        ...
        //Torna visível
        actionSalvar.setVisible(true);
    break;
    case R.id.action_salvar:
        ........
        ........
        //Torna invisível
        actionSalvar.setVisible(false);
    break;
    default:
    break;
    }
    return super.onOptionsItemSelected(item);
}
    
01.02.2016 / 18:36