Greetings. Not long ago, I began to study android and now the question arose (in the title). I would be very grateful for the help, as I still strongly “swim” in the abundance of classes and methods in the SDK.

enter image description here

public boolean onPrepareOptionsMenu(Menu menu) { //вызывается при вызове меню MenuItem action_mail = menu.findItem(R.id.action_item4); if (chb2.isChecked()){ action_mail.setVisible(true); supportInvalidateOptionsMenu(); }else{ action_mail.setVisible(false); supportInvalidateOptionsMenu(); } menu.setGroupVisible(R.id.groupID, chb1.isChecked()); return super.onPrepareOptionsMenu(menu); } 

    1 answer 1

    1. Override onPrepareOptionsMenu

       /** Called whenever we call supportInvalidateOptionsMenu() */ @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem itt = menu.findItem(R.id.menu_share); //здесь по условию меняйте меню return super.onPrepareOptionsMenu(menu); } 
    2. At the time of changing the condition, redraw the menu by calling

       supportInvalidateOptionsMenu() 

    Here you can also see:

    Access to the menu items in the toolbar

    • I tried on your advice. Maybe something is not understood, but the effect is such that the visibility of the icon does not change until I click on the menu and it will redraw only then. - Gerltsx
    • @Gerltsx, this is supportInvalidateOptionsMenu(); It is necessary to call not where you have, but where the change of the desired flag is processed. If you have a check-box - then in the listener the changes in his checkout - YuriySPb
    • Thanks for the help)) It turned out) - Gerltsx