How to add a Switch to a PopupMenu programmatically ( Without using XML )?

150 Views Asked by At

I want to add a Switch to a PopupMenu as an item, I also don't want to use any XML codes, Is there is any way to do this?

here is an example of my code :

options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
    PopupMenu myPopupMenu = new PopupMenu(getApplicationContext(), options);
    myPopupMenu.getMenu().add("Option 1");
    myPopupMenu.getMenu().add("Option 2");
    myPopupMenu.getMenu().add("Option 3");
    //Here I want to add a Switch
    myPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            
            public boolean onMenuItemClick(MenuItem item) {
                    switch(item.getTitle().toString()) {
                case "Option 1": {
                    onFirstOptionClicked();
                    break;
                }
                case "Option 2": {
                    onSecondOptionClicked();
                    break;
                }
                case "Option 3": {
                    onThirdOptionClicked();
                    break;
                }
                case "The Title of The Switch": {
                    //here i want to get a boolean value
                    //that detects if the Switch is checked or not
                    break;
                }
            }
                    return true;
            }
    });
    myPopupMenu.show();
}});

I tried using the setCheckable method but it made a CheckBox item, what I want is a Switch item :

myPopupMenu.getMenu().add("The Title of The Switch").setCheckable(true);
0

There are 0 best solutions below