Ram Ram - 5 months ago 11
Android Question

Alert Dialog was disappearing when user clicks out side

Hi everyone i have a alert box with two text box's, and here the problem is the alert dialog was disappearing when user clicks outside of that pop up or the Alert dialog is disappearing when user clicks Ok button too.

So please help me in this regards
Thanks in advance...

final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Sign In Failed");
final EditText input1=new EditText(MainActivity.this);
final EditText input2=new EditText(MainActivity.this);
input1.setHint("eNTER name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
builder.setMessage("Invalid username or password");
linearLayout.addView(input1);
linearLayout.addView(input2);
builder.setView(linearLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();

Answer

because by default it is Cancelable

Add this after builder.setView(linearLayout) -

builder.setCancelable(false);

UPDATE

As per your code snippet below-

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

on positive button ("OK") click, you are setting dialog.cancel() Don't do this, you should set some action as you required on positive button click.

See This :

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel(); // close the current dialog
        }
    });

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

           //Perform any Intent Action or perform validation as you want

         }
    });

UPDATE 2

Just copy & paste below code - working perfectly

 final EditText input1 = new EditText(MainActivity.this);
        final EditText input2 = new EditText(MainActivity.this);
        input1.setHint("Enter name1");
        input2.setHint("Enter Name2");
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(input1);
        linearLayout.addView(input2);

        final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
                .setTitle("Sign In Failed")
                .setCancelable(false)
                .setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();
        builder.show();
        builder.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (input1.length() <= 0) {
                    Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    builder.dismiss();
                }
            }
        });
Comments