Skip to content Skip to sidebar Skip to footer

How To Store User Name And Password Details In Strings.xml

I am developing an app, it has a login page. I need to store the login credentials. Can it be in my strings.xml file? Because I have heard that Strings.xml can not be modified at r

Solution 1:

You can store login information in SharedPreference or SqliteDatabase.

SharedPreferencessharedPreferences= getPreferences(MODE_PRIVATE);
        SharedPreferences.Editoreditor= sharedPreferences.edit();
        editor.putString("username", YOUR_USERNAME);
        editor.putString("password", YOUR_PASSWORD);
        editor.commit();

For retrieving Login information

SharedPreferencesprefs= getPreferences(MODE_PRIVATE); 
Stringusername= prefs.getString("username", null);
Stringpassword= prefs.getString("password", null);

If you need more security you can use SQLCipher using SqliteDatabase

Please go through this link.

Solution 2:

Use Shared Preferences. Like so:

Create these methods for use, or just use the content inside of the methods whenever you want:

publicStringgetUserName()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("userName","no userName created");
  return str;
}

publicStringgetPassword()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("password","no password created");
  return str;
}

publicvoidwriteToUserNameAndPassword(String userName, String password)
{
  SharedPreferences.Editor pref = 
                           getSharedPreferences("userNameAndPassword",0).edit();
  pref.putString("userName", userName);
  pref.putString("password", password);
  pref.commit();
}

You could call them like this:

// their userName if "foo" and their password is "bar"
writeToUserNameAndPassword("foo", "bar");

if (getUserName().equals(inputUserName) && getPassword.equals(inputPassword))
{
   // they have the right userName and password
}
elseif (getUserName().equals("no userName created")
         && getPassword().equals("no password created"))
{
   // these preference Strings for their userName/password have both not been created
}
elseif (getUserName().equals("no userName created"))
{
   // this preference String for their userName has not been created, // but the password has been
}
elseif (getPassword().equals("no password created"))
{
   // this preference String for their password has not been created, // but the userName has been
}
else
{
   // they entered the wrong userName and/or password
}

Some explanation (if needed):

"password" and "userName" are the 'key' Strings in the preference. So you reference those keys to obtain the String you put in there. It is a reference name for the String you put.

"userNameAndPassword" is the preference name. You use the preference name, "userNameAndPassword", to reference the preference you want to access.

"no password created" and "no userName created" are the Strings that the getString method will return if the preference doesn't have a String referenced to by "password" or "userName", meaning that it hasn't been created.

Another way to put it: they are the default values of the reference String. So if nothing has been put their instead, the method will return the default values. You have to set the default values.

So, for example, if no "password" String has been put into the "userNameAndPassword" preference (written to using putString), then the getPassword() method will return "no password created".

Solution 3:

As @Armit mentioned before, you can store the data in the SharedPreferences. Just be aware that this gets stored in a simple XML file that can be seen and modified with an editor. You should at least encrypt it or, better, not save it at all. Usually, you log in to a server or site and then save only the return token. You only use the token to connect again and you don't have to save the password in plain text.

Solution 4:

In simple words YOU CAN'T STORE OR CHANGE the content of strings.xml

But yes as User @amit said you can store these values in Shared Preferences

Or You can Use SQLite Database to store what ever you want learn sqlite

For example

for setting the Value

SharedPreferences.Editor prefEditor = getPreferences(MODE_PRIVATE).edit();
prefEditor.putInt(LAUNCH_COUNT, 1); // you can have multiple put (values)
prefEditor.commit();
prefEditor.apply();

For getting the value

SharedPreferencessp= getPreferences(MODE_PRIVATE);
intlaunchCount= sp.getInt(LAUNCH_COUNT, -1);

Post a Comment for "How To Store User Name And Password Details In Strings.xml"