I have a preference settings for which i have configured a dialog box pop up on click of preference using the below code.
Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.password_dialog);
passwordDialog.setTitle("Set new password.");
passwordDialog.setCancelable(true);
passwordDialog.show();
The dialog is coming fine. However In the dialog i have a password edit text and a confirm password edit text. If these matches with each other i need to save the password to preference. I do not know how to save the value to a preference on ok click in my custom dialog. Please let me know how to do this. Thank you for your time and help.
You can retrieve the preferences, and then commit information to it :
SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.edit().putString("passwordKey", editText.getText().toString()).commit();
To add information to a preference, start by calling edit() (returns an editor), add the value you want (key/value, like a Map), and never forget to call commit() (to commit the changes).
You can then access your value using
preferences.getString("passwordKey", defaultValue);
Related
I am attempting to do something I'm unsure of, and need someone to provide feedback.
I have 2 preferences. One of them, I want dependent on the other one's value.
Example: My "Notification Mode" Preference has 2 options: "Timer", and "Reminder"
If the User chooses the "Timer" option, I want my other Preference available to set time.
Otherwise, I want the Preference for setting the Timer to be disabled.
Here's what I've come up with inside my pref changed listener:
if (key.equals(PREF_NOTIFICATION_MODE)) {
Preference notifModePref = findPreference(key);
notifModePref.setSummary(sharedPreferences.getString(key, ""));
if(!notifModePref.equals("Timer Mode")) {
Preference timerDurationPref = findPreference(PREF_TIMER_DURATION);
timerDurationPref.onDependencyChanged(notifModePref, true);
} else if(notifModePref.equals("Timer Mode")) {
Preference timerDurationPref = findPreference(PREF_TIMER_DURATION);
timerDurationPref.onDependencyChanged(notifModePref, false);
}
}
I'm not sure if I'm doing this correctly, or if onDependencyChanged is proper.
I accomplished the Goal inside of if(key.equals) by referencing the Preference in question and using if statements and a combination of using onDependencyChanged(), and setting .isEnabled(boolean) as required.
I'm trying to gain access to a (custom) list preference and change its selected value (i.e. if index 3 is selected, change the selection to index 1). However, findPreference() can only be used inside a PreferenceActivity. I need to access the preference and change its selection inside a regular Activity. Is there a way this can be done? I don't see anything in SharedPreferences that I can use to change the selection, only a list preferences value.
This is how you do it,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference(this);
prefs.getString(PREF_KEY, "default");
I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}
I have an application which has a Prefernces Class and I want to know how could i make so that when the application is started the settings to be applied even before entering the preferences ( settings ) class. I have a getPrefs() void method which is called when i press "Save" Button in preference activity.
So, could you help me and tell what should I do the "default" preferences to be applied when entering the application ? (I need getprefs method from another class )
I would be grate if you could give me some advices or tips.Thank you !
To get an instance of the SharedPreferences from anywhere in your application use:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPrefences(context);
To set a value in the preferences, you need to call the editor for those preferences, then set the value for a key and finally commit the result. It can all be done in a single line:
prefs.edit().putString("myKey","myValue").commit();
This would store the string value myValue on a key named myKey and it will be accessible (after you commit) to any class if it has the application's context when it calls getDefaultSharedPreferences.
To retrieve the stored value you specify the key and a fallback value in case there is no preference set with that key:
prefs.getString("myKey","oops no value found");
I have an app which uses the user's location. I have a dialog(pic below) asking user's permission to "Allow" or "Disallow" the app to use the user's location ( dialog pops up the first time users opens the app after installation OR when user tries to use the location based service while using user location is "Disallow"-ed by the user).
I also use preference item(a checkbox)(pic below) in PreferenceActivity where the user can the toggle his preference.
To change the value of the sharedpreference I have use this code
public void onClick(DialogInterface dialog, int id)
{
sharedPrefs =getSharedPreferences("prefs",MODE_WORLD_WRITEABLE);
Editor editor = sharedPrefs.edit();
editor.putBoolean("locationPermission", true);
editor.commit();
}
I had expected the checkbox value to change automatically depending on the dialog selection as the key "locationPermission" holds the value to the checkbox. But it is not so.
Now how do I map the dialog(pic 1) selection to the checkbox value(pic 2)?
The issue was solved by using
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
instead of
sharedPrefs = getSharedPreferences("prefs", MODE_WORLD_WRITEABLE);
You can call addPreferencesFromResource in the onCreate of your PreferenceActivity so that your UI is populated from the preferences.
Also, you may want to make sure that your CheckBoxPreference has android:persistent-"true" in its XML definition.