I have a preference activity that has a language
as ListPreference
which displays the available language list. I can fill the list when onCreate is called, but I want to fill the list when the user clicks on it.
this is the java code
:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
addPreferencesFromResource(R.xml.settings);
} catch (Exception e) {
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
if((preference instanceof ListPreference) && (preference.getKey().equals("language"))){
ListPreference lp = (ListPreference)preference;
CharSequence[] entries = { "English", "French" };
CharSequence[] entryValues = {"1" , "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
return false;
}
}
and this is the settings.xml
(preference) :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">
<CheckBoxPreference android:key="enabled" android:title="Application Status" android:summary="Enable or disable the application" />
<ListPreference
android:key="language"
android:title="Language"
android:dialogTitle="Application language"
android:summary="Select the Application language"
/>
</PreferenceCategory>
</PreferenceScreen>
I searched but found no result! An exception occurs every time I click on that list.
I solved the problem my extending the ListPreference. It was very simple.
And then I just changed the name to my class in the XML.
And then I simply did this at the onCreate.
Worked right away. Actually, I never expected it would be done so easily.
Using PreferenceFragment & JAVA set key rather than
PreferenceActivity & XMLas shown in https://stackoverflow.com/a/13828912/1815624, which this answer is based on:If what you want is to be able to change the items in the list dynamically after the initial ListPreference object has been initialized then you will need to attach the
OnPreferenceClickListener
directly to the ListPreference object. Use the key you have specified in the JAVA source (asCUSTOM_LIST
) to get a handle to the preference.Since the code to populate the
entries
andentryValues
arrays will have to run both inonCreate()
and inonPreferenceClick
, it makes sense to extract it to a separate method -setListPreferenceData()
in order to avoid duplication.XML layout:
You are getting the exception because your ListPreference object is not fully initialized - you either need to set
entries
andentryValues
attributes in your XML or do it programatically inonCreate()
.If what you want is to be able to change the items in the list dynamically after the initial ListPreference object has been initialized then you will need to attach the
OnPreferenceClickListener
directly to the ListPreference object. Use the key you have specified in the XML to get a handle to the preference.Since the code to populate the
entries
andentryValues
arrays will have to be run both inonCreate()
and inonPreferenceClick
, it makes sense to extract it to a separate method -setListPreferenceData()
in order to avoid duplication.