0
public class MainActivity extends AppCompatActivity {


    private final int item_4 = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPrepareOptionsMenu();
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.menu_main, menu);


        menu.add(Menu.NONE, item_4, Menu.NONE, "Show");

       menu.add(Menu.NONE, item_4, Menu.NONE, "Hide");


        menu.add("Reset");




        return true;

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {




        switch (item.getItemId()) {
            case R.id.item1:

                Toast.makeText(this, "You Pressed Settings", Toast.LENGTH_SHORT).show();
                return true;


            case R.id.item2:
                Toast.makeText(this, "You Pressed Create", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.item3:
                Toast.makeText(this, "You Pressed Delete", Toast.LENGTH_SHORT).show();
                return true;


            case item_4:
                Toast.makeText(this, "You Pressed Hide", Toast.LENGTH_SHORT).show();
                return true;

            default:
                return super.onOptionsItemSelected(item);

        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem mii = menu.findItem(R.id.item2);
        mii.setEnabled(false);


        return super.onPrepareOptionsMenu(menu);
    }
}

In the above code I have created a menu item list for the app bar and I have created a button in my app and i want some of my items to be disabled after button click but I am not able to do so as I am not gettig how to call onPrepareOptionsMenu() on button click? Can anyone suggest a solution for how to disable the menu items after button click?

2 Answers2

0

I will show short example:

Only one note for this method: don’t forget to clear menu items (menu.clear();) on every method start.

Example:

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

* Class which shows how to change dynamically options menu items

*/

public class Main extends Activity {

private Button clickBtn;

private boolean isChangedStat = false;

private static final int MENUITEM = Menu.FIRST;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

clickBtn = (Button) findViewById(R.id.click);

clickBtn.setText("Click me");

clickBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if(isChangedStat) {

isChangedStat = false;

} else {

isChangedStat = true;

}

}

});

}

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

menu.clear();

if(isChangedStat) {

menu.add(0, MENUITEM, 0, "True");

} else {

menu.add(0, MENUITEM, 0, "False");

}

return super.onPrepareOptionsMenu(menu);

}

}
gabriele.pacor
  • 107
  • 1
  • 2
  • 9
0

Reference from: Android: How to enable/disable option menu item on button click?

Once the activity is created, the onCreateOptionsMenu() method is called only once, as described above. The system keeps and re-uses the Menu you define in this method until your activity is destroyed. If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method. This passes you the Menu object as it currently exists. This is useful if you'd like to remove, add, disable, or enable menu items depending on the current state of your application.

you could call the method invalidateOptionsMenu(); in your button's click listener. It will call onPrepareOptionsMenu() again, all your logic to disable could be written here.

Anyway, documents covers all.

Yanbin Hu
  • 537
  • 6
  • 18