Thev
Thev

Reputation: 1125

Password stored in SharedPreferences not working in android?

In this order, this is the procedure I'd like to implement.

1) When imageButton is pressed, it checks the value of "pass" stored in SharedPreferences. If this matches the password, the intent starts the next activity.

2) If it is null, or does not match the password, a dialog box pops up prompting username and password entry.

3) If the entered password is correct, it writes the username and password to SharedPreferences.

4) If the entered password is incorrect, it makes a Toast indicating as such.

Thus far, the login is working perfectly. However, I can't seem to get the SharedPreferences function to work. My code is below:

Button aliaLogin = (Button)findViewById(R.id.imageButton);
    aliaLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("username","7");
            editor.putString("pass","11");
            editor.commit();
            String username = prefs.getString("username",null);
            String pass = prefs.getString("pass",null);
            if (pass != null && !pass.isEmpty() && pass.equals(housePass[5])){
                Intent intent = new Intent(getApplicationContext(), DisplayHouse.class);
                intent.putExtra("Username", username);
                startActivity(intent);
            }
            else {

                final Dialog dialog = new Dialog(HouseMain.this);
                dialog.setTitle("Login Required!");
                dialog.setContentView(R.layout.login_toast);
                dialog.show();

                final EditText name = (EditText) dialog.findViewById(R.id.name);
                final EditText password = (EditText) dialog.findViewById(R.id.password);
                Button submit = (Button) dialog.findViewById(R.id.submitButton);
                Button cancel = (Button) dialog.findViewById(R.id.cancelButton);

                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String username = name.getText().toString();
                        String pass = password.getText().toString();
                        SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();

                        if (pass.equals(housePass[5])) {
                            Intent intent = new Intent(getApplicationContext(), DisplayHouse.class);
                            intent.putExtra("Username", username);
                            editor.putString("username", username);
                            editor.putString("pass", pass);
                            editor.commit();
                            startActivity(intent);
                        } else {
                            Toast.makeText(getApplicationContext(), "Incorrect! Impostor alert!", Toast.LENGTH_SHORT).show();
                        }

                        dialog.cancel();

                    }

                });
                cancel.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }

                });

            }


        }
    });

Could someone tell me what I'm doing wrong?

Upvotes: 0

Views: 254

Answers (2)

droidev
droidev

Reputation: 7390

The problem is each time when you click on the button you are resetting the user name and password to default

 SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username","7");
        editor.putString("pass","11");
        editor.commit();

you have to remove it from your code

Upvotes: 2

mdtuyen
mdtuyen

Reputation: 4548

You have override pass save to Preference file with other pass:

 SharedPreferences prefs = getSharedPreferences("testapp", MODE_PRIVATE);
 SharedPreferences.Editor editor = prefs.edit();
 editor.putString("username","7");
 editor.putString("pass","11");
 editor.commit();

Upvotes: 0

Related Questions