MNM MNM - 1 year ago 48
Java Question

Trying to store user information into a shared prefereance file. Android

I am trying to store user information into a shared preference file in android studio, but it does not write to the file right.If I make individual files for each variable I want to keep then it works just fine but when I try to put them all in just one file, it only write one of the input on both variables (not what I want

I don't know if I am missing something here.

Here is my shared preference class:

public class MySharedPerference {
//how to use
MYPERFERENCE.writeString(getApplicationContext(),MYPERFERENCE.VARIABLE, "Values you want to store");
****Use Preferences to Read value using:-****

MyData.readString(getApplicationContext(), MyData.USERNAME,"");


//this is the name of ther perfecne
public static final String MYPERFERENCE = "myprefs";

public static final int MODE = Context.MODE_PRIVATE;

// this is the varialbe that you want to store
public static final String VARIABLE = "";
public static final String MYNAME = "";
public static final String MYRANK = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
public static final String MYPOINTS = "";
public static final String MYGCMTOKEN = "";
public static final String MYBARCODE = "";
public static final String MYCUSTOMERNUMBER = "";

public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();

public static boolean readBoolean(Context context, String key,
boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);

public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();


public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);

public static void writeString(Context context, String key, String value) {
getEditor(context).putString(key, value).commit();


public static String readString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);

public static void writeFloat(Context context, String key, float value) {
getEditor(context).putFloat(key, value).commit();

public static float readFloat(Context context, String key, float defValue) {
return getPreferences(context).getFloat(key, defValue);

public static void writeLong(Context context, String key, long value) {
getEditor(context).putLong(key, value).commit();

public static long readLong(Context context, String key, long defValue) {
return getPreferences(context).getLong(key, defValue);

public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(MYPERFERENCE, MODE);

public static SharedPreferences.Editor getEditor(Context context) {
return getPreferences(context).edit();


And here is how I write to them

Log.d("My user name " , userName);
Log.d("My Password " , passWord)

MySharedPerference.writeString(getApplicationContext(),MySharedPerference .USERNAME, userName);
MySharedPerference.writeString(getApplicationContext(),MySharedPerference.PASSWORD, passWord);

and here is how I read form them:

String storedUser = MySharedPerference.readString(myActivity.getApplicationContext(),MySharedPerference.USERNAME, "");
String storedPass = MySharedPerference.readString(myActivity.getApplicationContext(), MySharedPerference.PASSWORD, "");

Any ideas?

Thank for any help with this

Answer Source

You're using an empty string for the key for both your values.

Give them a name e.g:

public static final String USERNAME = "username";
public static final String PASSWORD = "password";

These aren't default values, they are the strings that are used as the labels for the values in the Preference file when storing the values.

Storing passwords in the Preferences file may not be a great idea if you are worried about security, but it depends on how important the password is. As an alternative, consider using the Android Keystore System.