In my application I am using a login process. In that I have username and password field. Once I login, the username and password will be there even when you exit the app or kill the app.
How can I set like that?
In android There is functionality of sharedpreferences You can save your username and password in shared preferences and check that if sharedpreferences are not null then display username and password in textField
You can use SharedPreference as below:
SharedPreferences m_sharedPreference=getSharedPreferences("MyPrefs",MODE_PRIVATE);
SharedPreferences.Editor m_editor =m_sharedPreference.edit();
m_editor.putString("UserName", m_etUsername.getText().toString());
m_editor.putString("Password", m_loetPassword.getText().toString());
m_editor.commit();
Related
I have done login and registration activity using rest API. But in login I used only Mobile number editext. And in registration I used password. So I want to use that password in login. Means user should enter that password while he used during registraton.
So how to do that?
If you're going to store passwords, make sure you store them in EncryptedSharedPreferences. You do not have to encrypt them manually as the library will take care of it.
If you need/want more explanation on this and how it works, check out some articles about it on the web - there are plenty.
If your using Kotlin, saving password in Shared Preferences could look like this:
Initialize Shared Preferences:
var prefs: SharedPreferences
prefs = getSharedPreferences("name_of_your_file", Context.MODE_PRIVATE)
Save password to it:
with (prefs.edit()) {
putString("password", etPasword.text)
apply()
}
If you want to access this stored password, you can do it like this:
val password = prefs.getString("password", "default_value")
So basically I have an activity (SplashActivity) that serves as a splash screen. It reads a key named username from the application's SharedPreferences, then if it finds the key, starts HomePage activity. Otherwise it starts LoginActivity. When the login is successful in LoginActivity, I am storing the username and password via this code:
SharedPreferences prefs = LoginActivity.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();
The login page also has an autocomplete feature for the password field. This is how I am reading the password:
String password = this.getPreferences(MODE_PRIVATE).getString("password", "");
And I get the stored password. However, when I restart the app and run SplashActivity I can't read the password despite using the exact same code as above. Is there something wrong I am doing here?
Quoting the JavaDocs for getPreferences():
Retrieve a SharedPreferences object for accessing preferences that are private to this activity.
(emphasis added)
So, SplashActivity cannot read a value written to the private preferences of LoginActivity.
You probably want to switch to getSharedPreferences(), where you provide your own name that can be shared between the two activities.
Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant
I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();
Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.
Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.
In android how to search that sharedpreference contains some value or not?
Actually I m making application which takes password and confirm password as fields.when user start app for first time he must enter both password and confirm password. But i want when user restart that app he must ask to enter only password.
For that i store password in sharedPreferences but now how do i know that their is already password exists in sharedPreferences or not?
so that if their is no password in sharedPreferences i can show the activity which contains both password and confirm password to enter AND if there exists password then i wl show activity that contains only password to enter.
If Anyone have idea then please help me.I m tring from many days but still not getting the output.
You can check it by using the contains method on your SharedPreferences instance:
boolean hasPassword = preference.contains("passwordKey");
API Docs:
public abstract boolean contains (String key)
Since: API Level 1
Checks whether the preferences contains a preference.
Parameters
key The name of the preference to check.
Returns
Returns true if the preference exists in the preferences, otherwise false.
for saving data...
SharedPreferences settings = getSharedPreferences("YourKey", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("password", passwordValue);
// Don't forget to commit your edits!!!
editor.commit();
for retrieving...
SharedPreferences settings =this.getSharedPreferences("YourKey", 0);
String userData = settings.getString("password", "0");
if((userData.equals("0"))){
//password has not been saved...
}
else{
//password is already there...
}
fist check if passwort is set:
boolean password_exists = !settings.getString("password","").equals("");
then hide or show the field for the second password
second_passwort_edittext.setVisibility(password_exists ? View.GONE:View.VISIBLE);
After entering the password you can change the behaviour with password_exists (compare the passwords and set them if false, compare with given stored password if true)
I think that you could just validate if you have received something by getting the password value from your shared preferences
if(preferences.getInt("storedPass", 0) != null) {
//Do Stuff
}
I have a android application which needs username and password to login. I need to save the username and password locally in phone or somewhere to use them when the user opens the app next time and logins to the app automatically without showing the login screen
EditText input1 = (EditText) findViewById(R.id.usertext);
EditText input2 = (EditText) findViewById(R.id.Passtext);
String username = input1.getText().toString();
String password = input2.getText().toString();
If the login is successful, it will call the activity through intent.
If you are using ApiLevel >= 5 read about AccountManager.
I would recomend to use something like MD5 or SHA1 for hashing your password before storing.
A possible place to store could either be "preferences" or the sqlite DB (not such usefull for only one single dataset)
To save username & password in sharepref try
SharedPreferences.Editor editor=mPreferences.edit();
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
editor.putString("UserName", username);
editor.putString("PassWord", password);
editor.commit();
You can use preferences or a file if you don't really care about security. You should encrypt the password if you're gonna store them though.
See here for a better description of the options.