You may call dismiss(); on the dialog. This work for me.
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog Design.
setTitle(CharSequence title) AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.
This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Edit for complete solution...
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
builder.setPositiveButton("PositiveButton",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// DO TASK
}
});
builder.setNegativeButton("NegativeButton",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// DO TASK
}
});
// Set `EditText` to `dialog`. You can add `EditText` from `xml` too.
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
input.setLayoutParams(lp);
builder.setView(input);
final AlertDialog dialog = builder.create();
dialog.show();
// Initially disable the button
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
// OR you can use here setOnShowListener to disable button at first time.
// Now set the textchange listener for edittext
input.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// Check if edittext is empty
if (TextUtils.isEmpty(s)) {
// Disable ok button
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
// Something into edit text. Enable the button.
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
Below are edited history, which can be refer as some more details
Here is a sample code, try this
AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});
builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});
AlertDialog dialog = builder.create();
dialog.show();
// After calling show method, you need to check your condition and enable/disable the dialog buttons
if (your_condition_true) {
// BUTTON1 is the positive button
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);
}
For negative button
dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button
For buttons id : Reference alert_dialog.xml
Edited :
And the setOnShowListener since level 8 API (FroYo), does the same,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if (condition) {
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
});
dialog.show();
Edited
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();
None of these answers really solve the problem.
I accomplish this using a custom layout with an EditText in it and a TextWatcher on that view.
final LinearLayout layout = (LinearLayout) inflator.inflate(R.layout.text_dialog, null);
final EditText text = (EditText) layout.findViewById(R.id.text_edit);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
// Now add the buttons...
builder.setPositiveButton(R.string.ok, new AlertDialog.OnClickListener() {
// Left out for brevity...
}
builder.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
// Left out for brevity...
}
// Create the dialog
final AlertDialog d = builder.create();
// Now add a TextWatcher that will handle enable/disable of save button
text.addTextChangedListener(new TextWatcher() {
private void handleText() {
// Grab the button
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
if(text.getText().length() == 0) {
okButton.setEnabled(false);
} else {
okButton.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable arg0) {
handleText();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Nothing to do
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Nothing to do
}
});
// show the dialog
d.show();
// and disable the button to start with
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
Here is complete code to enable and disable positive button of dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = MainActivity.this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.dialog,null);
builder.setView(view);
builder.setTitle("Test");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Ok clicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("cancel", null);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
EditText editText = (EditText)view.findViewById(R.id.mobile_number);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (s.length() >= 1) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
});
You can write a listener to the edit text box, and try to enable or disable buttons. This is a sample code for xamarin.
var dialog = builder.Create();
dialog.Show();
var btnOk = dialog.GetButton((int)DialogButtonType.Positive).Enabled = false;
_enterTextDialogEditText.AfterTextChanged += (sender, e) => {
if (!string.IsNullOrEmpty(_enterTextDialogEditText.Text)) {
dialog.GetButton((int)DialogButtonType.Positive).Enabled = true;
} else {
dialog.GetButton((int)DialogButtonType.Positive).Enabled = false;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With