I have the application with coding key, that should be visible from all parts of the application. It has to have a default value and an ability to be changed from one of the activities. How can I set this default value?
Asked
Active
Viewed 244 times
-5
-
2Possible duplicate of [SharedPrefrences in Android](http://stackoverflow.com/questions/18442592/sharedprefrences-in-android) – Anand Singh Jan 13 '16 at 09:30
-
3try to do google before asking on stackover flow – Anjali Tripathi Jan 13 '16 at 09:31
-
I have googled it and it did`t help. – Arthur Arthur Jan 13 '16 at 09:33
-
your question is not clear . ask with proper explaination – Gopal Sharma Jan 13 '16 at 09:37
-
@ArthurArthur see my comment above, while getting value from sharedPrefrences try `String username = prefs.getString("username", defaultValue);` – Anand Singh Jan 13 '16 at 09:38
-
2Possible duplicate of [Android Shared preferences example](http://stackoverflow.com/questions/23024831/android-shared-preferences-example) – Soham Jan 13 '16 at 09:52
-
Please, learn how to google. Keywords: `android shared preferences`. – Phantômaxx Jan 13 '16 at 09:52
2 Answers
0
To save default value you have Two options . 1. SharedPreference 2. Database.
Try with SharedPreference
Create Helper class.
public class SharedPreferencesHelper {
private static final String TAG = "SharedPreferencesHelper";
Context context;
SharedPreferences sharedPreferences;
public SharedPreferencesHelper(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences("loginDetails",Context.MODE_PRIVATE);
}
/**
* To set login details
* @param userName : username to set
* @param password : password to set
*/
public void setLoginDetails(String userName, String password) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userName",userName);
editor.putString("password",password);
editor.commit();
}
/**
* To check and get login details
* @param userName : name to validate
* @param password : password to validate
* @return true : if valid user
* false : if valid password
*/
public boolean isValidUser(String userName, String password) {
// to get username
Log.d(TAG, "username = " + sharedPreferences.getString("userName", null));
Log.d(TAG, "password = " + sharedPreferences.getString("password", null));
if(sharedPreferences.getString("userName",null).equals(userName) && sharedPreferences.getString("password",null).equals(password))
return true;
else
return false;
}
}
To access from any where in application (activity / fragment)
SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(this);
sharedPreferencesHelper.setLoginDetails("admin","admin");
sharedPreferencesHelper.isValidUser("admin","admin");
This may help you.
0
Let's say this is your shared preferences:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
you can set the default value like this:
String username = sharedPreferences.getString("USER_NAME", "DEFAULT_VALUE");

Kh5
- 167
- 2
- 10