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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With