I have a EditText within a AlertDialog. I want to dismiss it only when the EditText has some input in it otherwise show a Toast prompting the user to enter the data.
Currently, when the edit text has data the dialog dismissed properly. Also when the edittext is blank the toast is shown. However the dialog still dismisses even if the EditText is empty and the Toast message is shown later.
I dont want the ALertDialog to dismiss and EditText should gain focus again after the Toast is shown.
Here is my code:-
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
boolean worked = true;
postedComment = input1.getText().toString();
if(postedComment.length()==0 || postedComment=="" || postedComment.matches(""))
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
worked = false;
}
else if(worked && postedComment!="")
{
dialog.dismiss();
pd = new ProgressDialog(NewsDetails.this);
pd.setMessage("Posting..");
pd.show();
pd.setCancelable(true);
PostComments(postedComment);
}
})
.setCancelable(false);
alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();