Home > OS >  How can I get username and password from SharedPreferences in Android?
How can I get username and password from SharedPreferences in Android?

Time:02-12

I am trying to recover the username and password to be able to start a session. These are the users of the sharedPreferences.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="admin">1234</string>
    <string name="user">user0987</string>
</map>

And this is the method

login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        SharedPreferences sp1 = MainActivity.this.getSharedPreferences("data", Context.MODE_PRIVATE);
        String user = sp1.getString(userInput.getText().toString(), "User not found");
        String pass = sp1.getString(passwordInput.getText().toString(), "Password is not correct");
        
        if(user.equals(userInput.getText().toString()){
            Toast.makeText(MainActivity.this, "User Exists", Toast.LENGTH_SHORT).show();
        } else{
            Toast.makeText(MainActivity.this, "User not found", Toast.LENGTH_SHORT).show();
        }

    }
});

This is the code that puts data in SharedPreferences

register.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       
        String user = userInput.getText().toString();
        String pass = passwordInput.getText().toString();

        if(user.isEmpty()){
            error.show();
        } else if (pass.isEmpty()){
            error2.show();
        } else{
            SharedPreferences sp=getSharedPreferences("data", Context.MODE_PRIVATE);
            SharedPreferences.Editor Ed=sp.edit();
            Ed.putString(user,pass);
            Ed.commit();
            Toast.makeText(MainActivity.this, "Register completed", Toast.LENGTH_SHORT).show();
        }

    }
});

CodePudding user response:

Please try to use next code when you retrieve data from SP:

SharedPreferences sp1 = MainActivity.this.getSharedPreferences("data", Context.MODE_PRIVATE);
String pass = sp1.getString(userInput.getText().toString(), null);
if (pass != null) {
    Toast.makeText(MainActivity.this, "User Exists", Toast.LENGTH_SHORT).show();
} else{
    Toast.makeText(MainActivity.this, "User not found", Toast.LENGTH_SHORT).show();
}

You are saving data in form of "user:pasword", so you need to get it the same way, using user name as the key.

CodePudding user response:

You need to add keys for saving for data in SharedPreferences editor.

Currently you are using entered username as key, It can be change according to entered data in edittext, because user will not enter same username every time, but for saving and getting data to/from SharedPreferences we need a specific key.

You can add keys for saving user name and password like following code:

 SharedPreferences sp=getSharedPreferences("data", Context.MODE_PRIVATE);
 SharedPreferences.Editor ed=sp.edit();
 ed.putString("user",user);
 ed.putString("pass",pass);
 ed.commit();

For getting data

SharedPreferences sp1 = getSharedPreferences("data", Context.MODE_PRIVATE);
String userName = sp1.getString("user", "User not found");
String password = sp1.getString("pass", "Password is not correct");

For more info regarding Sharedpref check Official documentation for Save key-value data using SharedPreferences

CodePudding user response:

This code works fine for now thanks to @Sergey

SharedPreferences sp1 = MainActivity.this.getSharedPreferences("data",Context.MODE_PRIVATE);
String pass = sp1.getString(userInput.getText().toString(), null);
if (pass != null) {
Toast.makeText(MainActivity.this, "User Exists", Toast.LENGTH_SHORT).show();
} else{
    Toast.makeText(MainActivity.this, "User not found", Toast.LENGTH_SHORT).show();
}
  • Related