Please look at the custom dialog below. I have an edittext field on the dialog and if the text field is empty I would like to disable the positiveButton
. I can get a charListener for the text field but I am not sure how I am going to set the positivebutton
to disable or enable from that listener? What is the reference for the positive and negative buttons?
case DIALOG_TEXT_ENTRY:
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}
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.
That might help you thanks.
This dialogFragment will do the job for you. Note that the dialog will remain open after screen rotation preserving any text that the user has already typed in. If you don't want that to happen you need to dismiss the fragment in your activity's onStop. The newInstance method signature can be changed to whatever you need.
Add implements to your activity (any type of Activity is fine):
Create the diaglogFragment in your activity like this:
Handle the result in your activity like this:
You need to create the resource identifier so add this resource under somewhere under res/values
Here is complete code to enable and disable positive button of dialog:
Edit for complete solution...
Here is a sample code, try this
For negative button
For buttons id : Reference alert_dialog.xml
Edited :
And the setOnShowListener since level 8 API (FroYo), does the same,
Edited
You can write a listener to the edit text box, and try to enable or disable buttons. This is a sample code for xamarin.