D
D
Danil Ochagov2018-11-20 15:25:35
Android
Danil Ochagov, 2018-11-20 15:25:35

Why resets the selected state of the ListPreference?

Hello, I have a settings activation (preferences.xml)
5bf3fc2d7e316812070687.png

<?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>

SettingsActivity.java
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);
        }
    }
}

And there is a function of changing the language. When the application is in English (default) language, then all values ​​are marked with a circle in ListPreferences, but when I change the language to Russian, everything crashes, and I have to set the values ​​myself, when I return to English, everything works fine. I noticed that setting the font size and changing the language remains to work as it should (mb that strings are written in the same language in all files)
strings-en.xml
<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>

strings-ru.xml
<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>

MainActivity.java
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);
    }

What is the problem? And is it worth doing so that initially the values ​​\u200b\u200bare displayed highlighted?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Chvarkov, 2018-11-20
@danilochagov


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 question

Ask a Question

731 491 924 answers to any question