qkflies
qkflies

Reputation: 31

How to use AlertDialog to prompt for PIN

So, I've researched quite a bit to try and implement a simple PIN authentication system for a Hello World game application. Unfortunately, I think a few things are still going over my head.

As my code currently stands, it shows the dialog box and does not allow it to be canceled. However, the EditText field no longer appears, which defeats the purpose of asking for a PIN. I think that a linked XML file will be needed for layout purposes, however, Android Studio does not seem to accept a call to such a file (i.e. pinauth.setView(findViewById(R.layout.dialog_pin));, which I don't think is the correct structure anyway.

Any assistance you can provide would be greatly appreciated. The code for the entire method is posted below.


public void auth(View v)
{

// The logout method works fine, since no PIN is necessary.
if(users.getBoolean("activeLogin", false) == true) successLogout();
    else
    {
        // Get selected user and pull SharedPreferences to get stored PIN.
        final String activeUser = userList.getSelectedItem().toString();
        scores = getSharedPreferences(activeUser, 0);

        // If a PIN is found on file, launch AlertDialog to prompt for PIN. No XML file is linked to the AlertDialog at this time.
        if(scores.getInt("pin", 123456789) != 123456789)
        {
            AlertDialog.Builder pinAuth = new AlertDialog.Builder(this);
            pinAuth.setTitle("PIN required.");
            pinAuth.setMessage("Please enter your pin.");
            pinAuth.setCancelable(false);
            pinAuth.setView(findViewById(R.layout.dialog_pin))
            final EditText pin = new EditText(this);
            pin.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);

            // These checks seem to work ok.
            pinAuth.setPositiveButton("Login", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(pin.getText().toString().isEmpty())
                    {
                        makeToast("Sorry, no pin was entered.", Toast.LENGTH_SHORT);
                        return;
                    }
                    else if(Integer.valueOf(pin.getText().toString()) != scores.getInt("pin", 123456789))
                    {
                        makeToast("Sorry, that pin was incorrect. Try again.", Toast.LENGTH_SHORT);
                        return;
                    }
                    else
                        successLogin(activeUser);

                }
            });

            // Show the AlertDialog.
            pinAuth.show();
        }

        // if the account has no PIN
        else successLogin(activeUser);
    }
}

As a side note, the PIN can only be up to 8 numeric characters, so 123456789 is an impossible PIN to create in the first place and therefore the conditional statement checking for the pin not to be equal to said number should not interfere with the operation.

Upvotes: 0

Views: 3501

Answers (1)

Steven_BDawg
Steven_BDawg

Reputation: 806

You need to inflate the custom layout first.

AlertDialog.Builder pinAuth = new AlertDialog.Builder(this);
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_pin, null);

pinAuth.setTitle("PIN required.");
pinAuth.setCancelable(false);
pinAuth.setView(view);

final EditText pin = (EditText) view.findViewById(R.id.whateverThisEditTextIdIs);
pin.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);

Note that I have also changed how you reference/create an EditText object. I have also taken away the use of pinAuth.setMessage() because your custom layout could be displaying that message.

Upvotes: 1

Related Questions