Android shared preferences retrieve username and password

14,184

Create Share Preference:

SharedPreferences sp=getSharedPreferences("Login", 0);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("Unm",Value );              
Ed.putString("Psw",Value);   
Ed.commit();

Get Value from Share preference:

SharedPreferences sp1=this.getSharedPreferences("Login",null);

String unm=sp1.getString("Unm", null);       
String pass = sp1.getString("Psw", null);
Share:
14,184
Worker123
Author by

Worker123

Updated on July 22, 2022

Comments

  • Worker123
    Worker123 almost 2 years

    Im having trouble with retrieving username and password from android's sharedpreferences. I use this code to save the username and pass

    SharedPreferences prefs=getSharedPreferences("File", 0);
        SharedPreferences.Editor e=  prefs.edit();
           e.putString("Email", "[email protected]").putString("Password", "password1");
           e.commit();
           e.putString("Email", "[email protected]").putString("Password", "password2");
           e.commit();
           String s=prefs.getString("Email","not found");
    

    But i dont know how to retrieve information for user to log in. Can anybody help me figure out

  • Worker123
    Worker123 about 12 years
    yes yes, i just had id figured out. Thank alot :) this is the solution i was looking for!
  • Hasmukh
    Hasmukh about 12 years
    ok that Good dear, if this is useful to you then accept answer..so it will help to other also..
  • LizG
    LizG about 6 years
    I have a driver app and a rider app in the same package. I am trying to get the current rider id from the driver app. I did as you did above but riderId is being returned as null? What am I doing wrong?
  • LizG
    LizG about 6 years
    App A: // .. create a shared preference - share current userid with Driver String currentRider = FirebaseAuth.getInstance().getCurrentUser().getUid(); SharedPreferences sp = getSharedPreferences("Current_User", 0); SharedPreferences.Editor ed = sp.edit(); ed.putString("rider", currentRider ); ed.commit(); App B: // .... Get Value from Share preference (RiderActivity)..... SharedPreferences sp1 = this.getSharedPreferences("Current_User", 0); riderId = sp1.getString("rider", null); Log.d("RIDERID", "RiderId = " +riderId);