0

I have a custom AlertDialog with an EditText for the PIN. OnClick of positive button the PIN in the editText is checked with the SharedPreferences. If it matches I want to close the dialog or else It should remain open. At the moment when the PIN is correct the dialog closes and re appears and I do not want it to re appear. Thanks for your help in advance.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub

    if ((System.currentTimeMillis() - mainScreenActivity.lastLoggedIn) / 1000 >= 120) {
        //startActivity(pinVarificationActivity);
        //Toast.makeText(getApplicationContext(),"Session has timed out, please enter your PIN",Toast.LENGTH_LONG).show();

        LayoutInflater inflaterPinVerificationDialog = this.getLayoutInflater();
        final View inflatorPinVerificationDialog = inflaterPinVerificationDialog.inflate(R.layout.dialog_pin_verification, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
        builder.setTitle("Session timed out. Please enter PIN");
        builder.setView(inflatorPinVerificationDialog);
        pinFromDialog = (EditText) inflatorPinVerificationDialog.findViewById(R.id.etDialogPin);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                String dialogPinValue = pinFromDialog.getText().toString();

                String sharedPrefPinVal = loginData.getString("pin", "not found");
                if (sharedPrefPinVal.equals(dialogPinValue)) {
                    Toast.makeText(getApplicationContext(), "login successful",
                            Toast.LENGTH_SHORT).show();                     
                    mainScreenActivity.lastLoggedIn = System.currentTimeMillis();
                    alertDialogPinVerification.dismiss();


                } else {
                    Toast.makeText(getApplicationContext(),
                            "Incorrect pin - Please try again",
                            Toast.LENGTH_LONG).show();
                }

            }
        });
        builder.setNegativeButton("Forgot PIN", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alertDialogPinVerification = builder.create();

        alertDialogPinVerification.show();

    } else {            
        mainScreenActivity.lastLoggedIn = System.currentTimeMillis();
    }
    return super.dispatchTouchEvent(ev);
}
BRDroid
  • 3,920
  • 8
  • 65
  • 143

1 Answers1

0

It looks as though you are doing this for every touch event. And if you are trying to only do this once, then you should perhaps check against the MotionEvent passed in and trigger the response on only a TouchDown for instance. You might be possibly calling multiple alert dialogs based on events that are triggered on touch down, touch up, move, etc. This would give the appearance of it opening again, but it is really because there are multiple behind each other.

@Override
    public boolean dispatchTouchEvent(MotionEvent ev) 
    {
        if(ev.getAction() == MotionEvent.ACTION_DOWN)
        {

In order to keep the alert window open, you can follow a couple suggestions as mentioned in these two articles: Tech Tips or Re-create AlertDialog. Both of which rely upon overriding and creating your own AlertDialog window.

Community
  • 1
  • 1
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
  • Hi Jay, thanks for your reply it worked for me now. At the moment it cancels the dialog with a toast message as incorrect pin. Can you suggest what to be done if i do not want to cancel the dialog when the PIN is wrong or how to re open the dialog for PIN entry – BRDroid Jan 27 '14 at 18:09