0

In my code, I'm trying to make an AlertDialog login, but I can not close it once the button is pressed, does anyone know any way? I tried creating an AlertDialog object from the builder, but it does not work. This is my code:

public class MainActivity extends AppCompatActivity {

ImageButton login;
Button signin;
EditText user, password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    login = (ImageButton)findViewById(R.id.login);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
            createSigninDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    });

}

public AlertDialog createSigninDialog (){

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = MainActivity.this.getLayoutInflater();

    View view = inflater.inflate(R.layout.dialog_signin, null);

    builder.setView(view);

    signin = (Button)view.findViewById(R.id.button_ingresar);
    user = (EditText)view.findViewById(R.id.user_input);
    password = (EditText)view.findViewById(R.id.password_input);

    signin.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
                    if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
                        Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
                        //Dismiss here
                    } else {
                        Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
                        //Dismiss here
                    }
                }
            }
    );

   return builder.show();
}
}
Alperen Kantarcı
  • 1,038
  • 9
  • 27

2 Answers2

1

Your function should look like that:

public AlertDialog createSigninDialog (){

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        LayoutInflater inflater = MainActivity.this.getLayoutInflater();

        View view = inflater.inflate(R.layout.dialog_signin, null);

        builder.setView(view);

        final AlertDialog dialog = builder.create();

        signin = (Button)view.findViewById(R.id.singing);
        user = (EditText)view.findViewById(R.id.user_input);
        password = (EditText)view.findViewById(R.id.password_input);


        signin.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
                            Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
                            //Dismiss here
                            dialog.dismiss();
                        } else {
                            Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
                            //Dismiss here
                            dialog.dismiss();
                        }
                    }
                }
        );

        return dialog;
    }

And then you can invoke it like that:

createSigninDialog().show();
Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
0

You should call builder.create() instead of calling builder.show(). It returns a dialog instance to you, then show() and dismiss() methods will be available to call.

aminography
  • 21,986
  • 13
  • 70
  • 74