-1

In my application I am showing AlertDialog.Builder

AlertDialog.Builder alert = new AlertDialog.Builder(this);

and in this alert there is one EditText and PositiveButton.

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Enter Text");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {
if(input.getTest().getText().toString().equalsIgnoreCase("")){
Toast.makeText(Activity.this, "Please enter some text", Toast.LENGTH_LONG).show();
}
}
alert.show();

If nothing is entered then it shows Please enter some text on toast and the dialog is closed but I want that Alert dialog to be closes. How can I achieve this?

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Vinit ...
  • 1,409
  • 10
  • 37
  • 66

2 Answers2

0

You need to create the dialog first and then show.

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            AlertDialog alertDialog = alert.create();
            alertDialog.show();
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
0
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Enter Text");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {}
}

final AlertDialog dialog = alert.create();
dialog.show();

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
      if(input.getTest().getText().toString().equalsIgnoreCase("")){
      Toast.makeText(Activity.this, "Please enter some text", Toast.LENGTH_LONG).show();
}
}
});
Vinit ...
  • 1,409
  • 10
  • 37
  • 66