0

I am using a DialogFragment to display a list of menu items in the dialog by setting the items based on a string array.

public class MenuDialogFragment extends 

    private Menu mMenu;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_title)
                .setItems(R.array.menu_array, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which) {
                            case 0:
                            // ...
                        }
                    }
                });

        return builder.create();
    }

What do I need to do, to deactivate (disable) one of the menu item entries? How can I get hold of them? Didn't find the right method in DialogFragment

Mahoni
  • 7,088
  • 17
  • 58
  • 115

1 Answers1

0
public class YourActivity extends Activity {
    private Menu menu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Create your menu...
        this.menu = menu;
        return true;
    }
    ....
}

Then use the following code whenever you want to disable the menu item:

menu.getItem(1).setEnabled(false);
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • Yeah, but when is menu not null? ;-) when I am using the builder it's still null. – Mahoni Sep 17 '14 at 08:57
  • can you post the code of when you are building your menu ? – Sagar Pilkhwal Sep 17 '14 at 08:59
  • I've added more code to give you a better idea about the whole DialogFragment, create dialog, set items, show problem. I know how to deactivate menu items in general. – Mahoni Sep 17 '14 at 09:26
  • try using `alertDialog.getListView();` like in this [SO post](http://stackoverflow.com/q/12946119/3326331) – Sagar Pilkhwal Sep 17 '14 at 09:32
  • This looks promising, but again it's available in onClick, that's too late. – Mahoni Sep 17 '14 at 10:11
  • yes you can move the `onClick()` code out of the block, move some edits to the code and then put the code just before you call `show()` on the dialog – Sagar Pilkhwal Sep 17 '14 at 10:12
  • Still null after a call to `show()`. I tried several method overloadings,.. no luck. The `listView` is there, but does not have any children before onClick happens. – Mahoni Sep 17 '14 at 10:35