1

I have created a alert dialog builder where am showing a form in dialogue my positive button name is submit I want the button to be disabled unless all the fields in the form are filled .Below is my code could anyone helpme awork around in this. Thanks

        alertDialogBuilder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    nameInput1 = data_txt1.getText().toString();
                    nameInput2 = data_txt2.getText().toString();
                    nameInput3 = data_txt3.getText().toString();
                    nameInput4 = data_txt4.getText().toString();
                    nameInput5 = data_txt5.getText().toString();

                    nameInput6 = auto_txt1.getText().toString();
                    nameInput7 = auto_txt2.getText().toString();
                    nameInput8 = auto_txt3.getText().toString();
                    nameInput9 = auto_txt4.getText().toString();
                    nameInput10 = auto_txt5.getText().toString();
                    nameInput11 = auto_txt6.getText().toString();
                    nameInput12 = auto_txt7.getText().toString();
                    nameInput13 = auto_txt8.getText().toString();
                    nameInput14 = auto_txt9.getText().toString();

                    nameInput15 = data_txt6.getText().toString();
                    nameInput16 = data_txt7.getText().toString();



                        Call<Void> completeQuestionnaireCall = spreadsheetWebService.completeQuestionnaire(nameInput1, nameInput2, nameInput3, nameInput4, nameInput5, nameInput6, nameInput7, nameInput8, nameInput9, nameInput10, nameInput11, nameInput12, nameInput13, nameInput14, nameInput15, nameInput16);
                        completeQuestionnaireCall.enqueue(callCallback);
                        dialog.dismiss();

                    }



                }
            });

    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();

        }
    });

    alertDialogBuilder.show();
}
Varun Kumar
  • 101
  • 4

6 Answers6

2

create a method that will check all your inputs in your Utils class or can use it in current class

public static boolean checkifEmptyText(String[] fields,Context context) {
    for (String currentField : fields) {
        if (currentField.getText().toString().trim().length() <= 0) {
            return false;
        }
    }
    return true;
}

now check it like this

if(checkifEmptyText(new String[]{all your strings}),context)
{
 Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(true);
}
else
{
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
}
Muhib Pirani
  • 765
  • 1
  • 6
  • 15
2

You can also do by this:

new AlertDialog.Builder(this)
    .setMessage("This may take a while")
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
         ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
         // the rest of your stuff
    }
})
    .show();
Krupa Suthar
  • 670
  • 3
  • 14
0

Try this

alertDialogBuilder.create().getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

ak sacha
  • 2,149
  • 14
  • 19
0

Use can do like this for positive button:

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

And, for negative button:

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
0

You can do this by this way :

    ....your code for alert dialog
    AlertDialog dialog = b.create(); // here b is reference of AlertDialog.Builder and in your case replace b with alertDialogBuilder
    dialog.show();
    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setEnabled(false);

Hope this will help.

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
0

If i understood your requirement correctly,

You can use a TextWatcher..

How it would work:
Add the textwatcher as a listener for text change to your inputfields, it would ensure a dynamic check on those for being 'filled' and you can associate the visibility of your button or enable/disable it accordingly

Questions which would help:
1. Disable button when edittext fields are empty
2. Use on text changed listener

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51