BRDroid
BRDroid

Reputation: 4388

Enable/disable positive button in single choice alert dialog

If you could help me with this, I have a single choice alert dialog with 3 choices. I disabled the NEXT button initially (none of the choices are selected). When the user selects any of the choices, I want to enable NEXT button. This is the code I tried.

int involvementInIncident;

case DIALOG_ADD_A_PERSON_INVOLVEMENT_ONE: 

        builder.setTitle("Involvement in this Incident");   
        builder.setSingleChoiceItems(incidentInvolvement, -1,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        involvementInIncident = which;                          
                        //involvementInIncident = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                        Toast.makeText(getApplicationContext(),""+ involvementInIncident, Toast.LENGTH_SHORT).show();


                    }
                });         
        builder.setPositiveButton("Next",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {                    
                        //Toast.makeText(getApplicationContext(),""+ involvementInIncident, Toast.LENGTH_SHORT).show();
                        showDialog(DIALOG_ADD_A_PERSON_PERSON_TYPE_TWO);

                    }

                });
        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();


                    }
                });     
        AlertDialog alertInvolvement = builder.create();
        alertInvolvement.show();             
        final Button buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE);
        buttonPositiveInvolvement.setEnabled(false);

        if(involvementInIncident == 0 || involvementInIncident == 1 || involvementInIncident==2){
            buttonPositiveInvolvement.setEnabled(true);
            //Toast.makeText(getApplicationContext(),"person responsiblee", Toast.LENGTH_SHORT).show();

        }
        else
        {
            buttonPositiveInvolvement.setEnabled(false);
        }

        return dialog;

Your help much appreciated. Thank you

Upvotes: 3

Views: 3987

Answers (3)

BRDroid
BRDroid

Reputation: 4388

Got it working finally. Thanks Piyush and Mystic Magic for your help. Posting the code here, might help others. Globally initialized buttonPositiveInvolvement Button buttonPositiveInvolvement;

to summarize: Enable/Disable Positive button in single choice AlertDialog when item is selected. (Initially the NEXT button is disabled, when an item is selected it enables the NEXT button)

//globally Button buttonPositiveInvolvement;

case DIALOG_ADD_A_PERSON_INVOLVEMENT_ONE:

        builder.setTitle("Involvement in this Incident");   
        builder.setSingleChoiceItems(incidentInvolvement, -1,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {                                            
                        //to enable the the NEXT button
                        buttonPositiveInvolvement.setEnabled(true);

                    }
                });         
        builder.setPositiveButton("Next",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {                    

                        showDialog(DIALOG_ADD_A_PERSON_PERSON_TYPE_TWO);

                    }

                });
        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();


                    }
                });             
        //This sequence needs to be followed to get it working or else it might screw up
        AlertDialog alertInvolvement = builder.create();            
        alertInvolvement.show();
        buttonPositiveInvolvement = alertInvolvement.getButton(AlertDialog.BUTTON_POSITIVE);            
        buttonPositiveInvolvement.setEnabled(false);

        return dialog;

Upvotes: 6

dipali
dipali

Reputation: 11188

public class MainActivity extends Activity {

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

    int call = 0;

    public void calldialog(final int call1) {
        call = call1;
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("Alert dialog title");
        builder.setMessage("Dialog message");
        builder.setCancelable(false);

        builder.setPositiveButton("cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        builder.setCancelable(false);
                        call = 1;
                        calldialog(call);
                    }
                });
        builder.setNegativeButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                call = 1;
                calldialog(call);
            }
        });
        builder.setNeutralButton("next", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                call = 0;
                calldialog(call);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

        // After calling show method, you need to check your condition and
        // enable/ disable buttons of dialog
        if (call == 0) {
            dialog.getButton(Dialog.BUTTON_NEUTRAL).setEnabled(false); // BUTTON1
        } else {
            dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false); // BUTTON1
            dialog.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false); // BUTTON1
        }
    }
}

i have search many more.....then i will give some logic as per your requriment..i think this code works perfect as per your requirement.

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You need to enable the button in onClick like this:

@Override
public void onClick(DialogInterface dialog, int which) {
    //your other code for toast
    buttonPositiveInvolvement.setEnabled(true);
}

So that will enable the button on selection of any choice.

Hope this helps.

Upvotes: 0

Related Questions