I am trying to add some text validation to an edit text field located within an alert dialog box. It prompts a user to enter in a name.
I want to add some validatio
I think you should recreate the Dialog
, as it seems the DialogInterface
given as a parameter in onClick()
doesn't give you an option to stop the closure of the Dialog
.
I also have a couple of tips for you:
Try using Activity.onCreateDialog()
, Activity.onPrepareDialog()
and of course Activity.showDialog()
. They make dialog usage much easier (atleast for me), also dialog usage looks more like menu usage. Using these methods, you will also be able to more easilty show the dialog again.
I want to give you a tip. It's not an answer to your question, but doing this in an answer is much more readable.
Instead of holding a reference to an AlertDialog.Builder()
object, you can simply do:
new AlertDialog.Builder(this)
.setTitle("Record New Track")
.setMessage("Please Name Your Track:")
//and some more method calls
.create();
//or .show();
Saves you a reference and a lot of typing ;). (almost?) All methods of AlertDialog.Builder
return an AlertDialog.Builder
object, which you can directly call a method on.
The same goes for Toast
s:
Toast.makeText(this, "Please enter...", Toast.LENGTH_LONG).show();