I have some list preferences, but I don't know how to save the individual values from the list. How do I do it? Here is what I have
http://i41.tinypic.com/dh4gvo.png
Preference customPref = (Preference) findPreference("notificationPref");
customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
SharedPreferences customSharedPreference = getSharedPreferences(
"notifications", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference
.edit();
editor.putString("notification",
"The preference has been clicked");
editor.commit();
return true;
}
});
my list click listener is only for the main item in the list preferences page, but not the items in the popup itself. How do I save the choice selected in the popup itself?
This is usually automatic. In your preference screen XML, you should have something like this:
<ListPreference android:title="#string/Title"
android:summary="#string/Summary"
android:key="PreferenceKey"
android:defaultValue="VALUE_2"
android:entries="#array/Entries"
android:entryValues="#array/Values" />
And in your strings.xml:
<string name="Value1">Text for value 1</string>
<string name="Value2">Text for value 2</string>
<string name="Value3">Text for value 3</string>
<string-array name="Entries">
<item>#string/Value1</item>
<item>#string/Value2</item>
<item>#string/Value2</item>
</string-array>
<string-array name="Values">
<item>VALUE_1</item>
<item>VALUE_2</item>
<item>VALUE_3</item>
</string-array>
The "Values" array specify the (string) value saved in preferences, whereas the "Entries" array specify the text of the items displayed to the user. Each time the user select an item, its corresponding value in the "Values" array is saved to preferences under the specified key ("PreferenceKey" in this example).
You can read preferences like this...
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("key", "Default Value");
Related
How can I save the current selected spinner value, such that when I reopen the application the saved value is automatically selected by default?
Please Write below code on onItemSelectedListener() of spinner and store selected value into shared preferences.
String mSpnValue=mSpinner1.getSelectedItem().toString();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Value", mSpnValue);
Use below code for set item as selected in spinner.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String myString = myPrefs.getString("Value","nothing"); // the value you want the
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) mSpinner1.getAdapter();
int spinnerPosition = myAdap.getPosition(myString);
// set the default according to value
mSpinner1.setSelection(spinnerPosition);
You can save spinner position in preferences and when entering back use spinner.setSelection(position_from_preferences);
You can use several methods
For example you can use a DataBase and save on it.
Other methods, and the best IMO, is used SharedPreferences
http://developer.android.com/intl/es/reference/android/content/SharedPreferences.html
http://developer.android.com/intl/es/reference/android/app/backup/SharedPreferencesBackupHelper.html
For set value one another way:---
for(int i=0;i<adapter.getCount();i++){
if(adapter.getItem(i).equals(your save preference value){
spinner_timer.setSelection(i);
}
}
you can also refer to the spinner value by position. that way you need only to deal with ints straight-fowardly:
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("VALUE", spinner.getSelectedItemPosition());
editor.commit();
and to load:
spinner.setSelection(settings.getInt("VALUE", 0));
I have ListPreference and it contains for example 5 options and I want to save one of this value to SharedPreferences when user selects it. How can I do it?
btw. I know how to save value to SharedPreferences, but I don't know how to get that value when user selects one of them.
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// newValue is the value you choose
return true;
}
};
listPreference.setOnPreferenceChangeListener(listener);
I access mine like this.. Please see example below
In my preference.xml file:
<ListPreference
android:key="SQS_ENDPOINT"
android:dialogTitle="Choose an option please"
android:entries="#array/sqsItems"
android:entryValues="#array/sqsValues"
android:title="SQS Endpoints" >
</ListPreference>
my String.xml:
<string-array name="sqsItems">
<item>US East (N. Virginia)</item>
<item>Asia Pacific (Singapore)</item>
<item>Asia Pacific (Tokyo)</item>
</string-array>
<string-array name="sqsValues">
<item>sqs.us-east-1.amazonaws.com</item>
<item>sqs.ap-southeast-1.amazonaws.com</item>
<item>sqs.ap-northeast-1.amazonaws.com</item>
</string-array>
And then I get the selected value like this from anywhere:
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
String END_POINT = pref.getString("SQS_ENDPOINT", "");
In your xml file you provide SharedPreferences key for your list.
<ListPreference
android:key="SHARED_PREFS_KEY"
...
/>
Every time user selects item from the list it is saved to the default SharedPreferences
i am new to android programming .well in my application i have added the preference class and i am calling the xml preference which is stored in the xml folder of my application.this is the code for it
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:title="colors" android:key="colors"
android:summary="list of colors to choose from " android:entries="#array/list"
android:entryValues="#array/lvalues"></ListPreference>
</PreferenceScreen>
and in the java class i had written only one line that is this
addPreferencesFromResource(R.xml.prefs);
now i am able to show the preference class and also the list some colors data.
my question is on selecting the list preference it should show me the selected preference in to the summary attribute .currently it is showing me as 'list of colors to choose from'
what i want is to show the selected color say i have selected white the summary should show me as white.
please help me .
Thank you,
maddy.
you can actually do it in xml. Try this:
<ListPreference
android:key="pref_list"
android:title="A list of preferences"
android:summary="%s"
android:entries="#array/pref_list_entries"
android:entryValues="#array/pref_list_entries_values"
android:defaultValue="0" />
your selected value would replace "%s".
For further clarification read this post
In the other code example there is a little bug which causes that the old value is displayed and not the new one.
Here is a working version:
ListPreference splashList = (ListPreference) findPreference("splash");
splashList.setSummary(splashList.getEntry());
splashList.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String nv = (String) newValue;
if (preference.getKey().equals("splash")) {
ListPreference splashList = (ListPreference) preference;
splashList.setSummary(splashList.getEntries()[splashList.findIndexOfValue(nv)]);
}
return true;
}
});
You need to extend ListPreference class and specify that in the xml file
class MyListPref extends ListPreference {
public void onClick (DialogInterface dialog, int which) {
this.setSummary(MyListPref.this.getEntry());
}
}
If your Custom Preference class name is com.sample.MyListPref the xml entry will be
<com.sample.MyListPref
android:key="pref_list"
android:title="List Preference"
android:dialogTitle="List Pref Dialog"
android:entries="#array/pref_items"
android:entryValues="#array/pref_items_values"/>
You dont need to do the extend listPreferences. in your PreferencesActivity file put as per my example:
final ListPreference defaultTown=(ListPreference)findPreference(getString(R.string.pref_default_town_key));
Log.i("try",defaultTown.getKey());
defaultTown.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.i("theApp", "Selected = " + defaultTown.getValue());
defaultTown.setTitle(defaultTown.getValue());
return true;
}
});
on change the value will update the title
when you enter the application you will need to populate the title with your current preference though instead of colors which is set at default in your xml
I have ListPreference that has 4 choices/options, I want to check for the selected option and make some code(if 1 is selected I do that, if 2 is selected I do other thing ...).
XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="kernel">
<item>TalonDev</item>
<item>Semaphore</item>
<item>SpeedMod</item>
<item>Galaxian</item>
</string-array>
<string-array name="kernel_return">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
Is that true :
choice = prefs.getString("listPref_kernel", "0");
if (choice == "0") {
try {
...................
If you are in a PreferenceActivity:
Retrieve your shared preferences:
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
and retrieve the value:
String value = sp.getString(key, "default");
Optionally, you can set a SharedPreferences.OnSharedPreferenceChangeListener via
sp.registerOnSharedPreferenceChangeListener(...)
to be notified on any change.
The XML file of my ListPreference
<ListPreference android:key="lpBirim" android:title="Birim"
android:summary="" android:defaultValue="0" android:persistent="false"/>
How to get the selected text and the selected value?
in your PreferenceActivity do something like:
ListPreference listPreference = (ListPreference) findPreference("lpBirim");
CharSequence currText = listPreference.getEntry();
String currValue = listPreference.getValue();
You can use this snippet to get the value:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.getString("lpBirim","-1")
Have look on the tutorial
Here is an example:
#Override
public boolean onPreferenceChange(Preference preference, Object value)
{
String textValue = value.toString();
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(textValue);
CharSequence[] entries = listPreference.getEntries();
if(index >= 0)
Toast.makeText(preference.getContext(), entries[index], Toast.LENGTH_LONG);
return true;
}
index contains the index of the clicked item
textValue is the Selected Value
entries[index] is the Selected Text
SharedPreferences Preference = PreferenceManager.getDefaultSharedPreferences(this);
Preference.getString("your list preference key","-1")
You can use findPreference() to get a ListPreference that has all methods you need. To have it working you need to use or extend PreferenceFragment first.