Answer the question
In order to leave comments, you need to log in
Why resets the selected state of the ListPreference?
Hello, I have a settings activation (preferences.xml)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/main_preference">
<CheckBoxPreference
android:defaultValue="true"
android:key="sounds_pref"
android:title="@string/sounds_preference" />
<ListPreference
android:entries="@array/font_size_list_description"
android:entryValues="@array/font_size_list_description"
android:key="font_size_description_pref"
android:summary="@string/font_size_preference_description"
android:title="@string/font_size_preference" />
<ListPreference
android:entries="@array/font_style_list_description"
android:entryValues="@array/font_style_list_description"
android:key="font_style_description_pref"
android:summary="@string/font_style_preference_description"
android:title="@string/font_style_preference" />
<ListPreference
android:title="@string/language_preference"
android:summary="@string/lang_preference_description"
android:key="lang_pref"
android:entries="@array/lang"
android:entryValues="@array/lang" />
</PreferenceCategory>
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.preferences, false);
}
public static class PreferencesFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
/* -----------set default value-------------- */
// font-size
ListPreference listPreference = (ListPreference) findPreference("font_size_description_pref");
if (listPreference.getValue() == null) listPreference.setValueIndex(4);
// font-style
ListPreference listPreference2 = (ListPreference) findPreference("font_style_description_pref");
if (listPreference2.getValue() == null) listPreference2.setValueIndex(1);
// sex of a person
ListPreference listPreference3 = (ListPreference) findPreference("sex_pref");
if (listPreference3.getValue() == null) listPreference3.setValueIndex(0);
// language
ListPreference listPreference4 = (ListPreference) findPreference("lang_pref");
if (listPreference4.getValue() == null) listPreference4.setValueIndex(0);
}
}
}
<string-array name="font_size_list_description">
<item>12sp</item>
<item>13sp</item>
<item>14sp</item>
<item>15sp</item>
<item>16sp</item>
<item>17sp</item>
<item>18sp</item>
</string-array>
<string-array name="font_style_list_description">
<item>italic</item>
<item>normal</item>
<item>bold</item>
<item>bold + italic</item>
</string-array>
<string-array name="sex">
<item>male</item>
<item>female</item>
</string-array>
<string-array name="lang">
<item>English(en)</item>
<item>Русский(ru)</item>
</string-array>
<string-array name="font_size_list_description">
<item>12sp</item>
<item>13sp</item>
<item>14sp</item>
<item>15sp</item>
<item>16sp</item>
<item>17sp</item>
<item>18sp</item>
</string-array>
<string-array name="font_style_list_description">
<item>курсивный</item>
<item>обычный</item>
<item>жирный</item>
<item>жирный + курсивный</item>
</string-array>
<string-array name="sex">
<item>мужской</item>
<item>женский</item>
</string-array>
<string-array name="lang">
<item>English(en)</item>
<item>Русский(ru)</item>
</string-array>
private SharedPreferences sharedPreferences;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
setLanguage();
setContentView(R.layout.activity_main);
}
private void setLanguage() {
String lang_code = "en";
switch (sharedPreferences.getString("lang_pref", "English(en)")) {
case "English(en)":
lang_code = "en";
break;
case "Русский(ru)":
lang_code = "ru";
}
Resources res = getResources();
DisplayMetrics displayMetrics = res.getDisplayMetrics();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(new Locale(lang_code.toLowerCase()));
} else {
configuration.locale = new Locale(lang_code.toLowerCase());
}
res.updateConfiguration(configuration, displayMetrics);
}
Answer the question
In order to leave comments, you need to log in
android:entries="@array/font_style_list_description"
android:entryValues="@array/font_style_list_description"
In the first one you insert strings that depend on the language and are displayed to users, and in the second - something that will be the same for all languages. Otherwise, when switching to Russian, it looks for, for example, the value "normal" among entryValues, does not find it and selects the first one
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question