Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match password using custom dialog box

Tags:

android

dialog

I want to use a custom dialog box for Pin Authentication. Here is my code:

public void fetchUI()
{
    add=(Button)findViewById(R.id.pinButton);

    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            final EditText input = new EditText(MainActivity.this);
            input.setTransformationMethod(PasswordTransformationMethod.getInstance());
            alert.setView(input);
            alert.setTitle("Type Your PIN");
            alert.setIcon(R.drawable.ic_launcher);
            alert.setMessage("Please Type Your PIN  below to Authenticate");

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

                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });

           alert.show();
        }
    });
}

Now I want to do this: if I click on OK with the correct PIN, then a dialog box disappears. Otherwise, if I click on OK, it won't disappear. How can I achieve this? Any help will be appreciated.

like image 995
Usman Kurd Avatar asked Nov 16 '25 14:11

Usman Kurd


1 Answers

    final String CORRECT_PIN = "123123"; // Should come from somewhere else than a local variable
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            if (CORRECT_PIN.equals(input.getText().toString()) {
                dialog.dismiss();
            } else {
                input.setError(getString(R.string.error_incorrect_pin));
            }    
        }
    });

Edit: Above code give a proper way of handling the validation. However, to prevent the dialog from getting dismissed after button is clicked, you will need to include your own buttons in a custom dialog button.

Other possibility: enable the OK button only when the pin is correct. You'll need to add a text change listener on your EditText and in the onTextChange method, do the following:

input.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {               
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(CORRECT_PIN.equals(input.getText().toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});
like image 57
Vincent Mimoun-Prat Avatar answered Nov 19 '25 03:11

Vincent Mimoun-Prat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!