How to retrieve the preference from ListPreference? - android

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.

Related

Android generate xml elements based on array

In WPF we can define an array and bind it to our xml. Elements are then automatically generated based on the contents of this array.:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
this.DataContext = this;
}
ObservableCollection<int> sampleData = new ObservableCollection<int>();
public ObservableCollection<int> SampleData {
get {
if (sampleData.Count <= 0) {
sampleData.Add(1);
sampleData.Add(2);
sampleData.Add(3);
sampleData.Add(4);
}
return sampleData;
}
}
}
<Window x:Class="Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox ItemsSource="{Binding Path=SampleData}"/>
</Grid>
</Window>
Is something like this possible in android?
You can define an XML file, in res/values/strings.xml, to define your array member using:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
More about array resource
Then receive using
getResources().getStringArray(R.array.planets_array);
But there is no
automatically generated based on the contents of this array
similar method in Android. To do it you will need to implement an ArrayAdapter.
More about ArrayAdapter

PreferenceActivity MultiSelectListPreference uncheck all

When first time I'm opening MultiSelectListPreference or when uncheck all check boxes and reopen MultiSelectListPreference, all items are displaying as checked.
I need to uncheck all check boxes on above situations.
How can I solve this?
settings.xml
<MultiSelectListPreference
android:entries="#array/pref_color_list_titles"
android:entryValues="#array/pref_color_list_values"
android:key="prefColor"
android:summary="#string/pref_color_summary"
android:title="#string/pref_color_title" >
</MultiSelectListPreference>
strings.xml
<string-array name="pref_color_list_titles">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
</string-array>
<string-array name="pref_color_list_values">
<item>r</item>
<item>g</item>
<item>b</item>
</string-array>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
You need just to pass empty Set to MultiSelectListPreference
Set<String> uncheckValues = new HashSet<>();
myMultiSelectListPreference.setValues(uncheckValues);
If you want check all values, you can do this one
String[] defaultValuesAsArray = getResources().getStringArray(R.array.default_values_multiselectlist_pref);
Set<String> defaultValues = new HashSet<>(Arrays.asList(defaultValuesAsArray)); //convert array to set
myMultiSelectListPreference.setValues(defaultValues);

How get selected option from ListPreference?

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

Android listpreferences, how to save individual preference

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");

how to read particular key value in string.xml on android?

string.xml contains lot of keys with value. I want to get particular value based on key.
For example, string.xml contains key1, key2 up to key100. I displaying these keys into ListView. If user select the key50 means I want to displaying the corresponding value in TextView. How to achieve this?
Sample string.xml file,
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="key1">value 1</string>
<string name="key2"> value 2 </string>
...
...
<string name="key100"> value 100 </string>
</resources>
You should save your key as a string array resource like this
<string-array name="my_keys">
<item>Key 1</item>
<item>Key 2</item>
<item>Key 3</item>
<item>Key 4</item>
<item>Key 5</item>
<item>Key 6</item>
</string-array>
Populate the listView with the following code.
String[] myKeys = getResources().getStringArray(R.array.my_keys);
ListView mListView = (ListView)findViewById(R.id.yourListView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));
Retrieve the clicked item and put the value into the TextView
TextView myTextView = (TextView)findViewById(R.id.yourTextView);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myTextView = ((TextView) view).getText();
}
});
If you have a <string name="key">string value</string> resources then the only way is to access them by key:
String value = context.getString(R.string.key);
Btw, if you need to store some arbitrary data into your Views (the ones inside your ListView), use view.setTag(object) and view.getTag().
I will suggest to use XPath to get your value for keys.

Categories

Resources