Reputation: 101
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();
}
Upvotes: 1
Views: 2301
Reputation: 680
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();
Upvotes: 2
Reputation: 785
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);
}
Upvotes: 2
Reputation: 8134
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
Upvotes: 0
Reputation: 3285
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);
Upvotes: 0
Reputation: 11545
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.
Upvotes: 0
Reputation: 2179
Try this
alertDialogBuilder.create().getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
Upvotes: 0