2

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.

Peter
  • 13,733
  • 11
  • 75
  • 122
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86

6 Answers6

5
    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) {
    }
});
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
2

An AlertDialog always dismisses when clicking on the PositiveButton or NegativeButton.

What you want to do is add your own "OK" and "Cancel" buttons to the View of the AlertDialog and have listeners on those buttons. So instead of just calling...

alert.setView(input);

... you want to also add your buttons. Something like this:

LinearLayout ll = new LinearLayout(context);
ll.addView(input);
ll.addView(okButtonYouInstansiatedYourself);
ll.addView(cancelButtonYouInstansiatedYourself);
alert.setView(ll);

okButtonYouInstansiatedYourself.b.setOnClickListener(...);
cancelButtonYouInstansiatedYourself.b.setOnClickListener(...);
stromvap
  • 213
  • 1
  • 7
2

Due To Various Relevant issues i`ve Changed The Approach that will act same as Dialog box

my Solution to this thing is

Use 2 Activites

  • 1 parent
  • 2 Child

call child from parent and do the work as per your requirement but in menifest file add these thing to your child activity your activity will behave like Dialog box

android:theme="@android:style/Theme.Dialog"

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
1

You can add an onShowListener to the AlertDialog where you can then override the onClickListener of the button.

final AlertDialog d = new AlertDialog.Builder(context)
        .setView(v)
        .setTitle(R.string.my_title)
        .setPositiveButton(android.R.string.ok,
                new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface d, int which) {
                        //Do nothing here. We override the onclick
                    }
                })
        .setNegativeButton(android.R.string.cancel, null)
        .create();

d.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Do something

                //Dismiss once everything is OK.
                d.dismiss();
            }
        });
    }
});

Source : How to prevent a dialog from closing when a button is clicked

Community
  • 1
  • 1
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
0
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            String val = input.getText().toString();  /// this is your input editbox value

            }
        });
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0

You can use SharedPreference to save username and password. Also you can check your password and user name through SharedPreference.

Arabin07
  • 11
  • 3