0

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 run time. So where can I store data i.e. User details or application details?

dcaswell
  • 3,137
  • 2
  • 26
  • 25
user2897471
  • 55
  • 1
  • 8

4 Answers4

3

You can store login information in SharedPreference or SqliteDatabase.

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

For retrieving Login information

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String username = prefs.getString("username", null);
String password = prefs.getString("password", null);

If you need more security you can use SQLCipher using SqliteDatabase

Please go through this link.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
1

Use Shared Preferences. Like so:

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

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

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

public void writeToUserNameAndPassword(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
}
else if (getUserName().equals("no userName created")
         && getPassword().equals("no password created"))
{
   // these preference Strings for their userName/password have both not been created
}
else if (getUserName().equals("no userName created"))
{
   // this preference String for their userName has not been created, 
   // but the password has been
}
else if (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".

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

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

SharedPreferences sp = getPreferences(MODE_PRIVATE);
int launchCount = sp.getInt(LAUNCH_COUNT, -1);
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

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.

SimonSays
  • 10,867
  • 7
  • 44
  • 59