androidlistpreference

Update ListPreference summary on language change


When a new language is selected in my ListPreference, I change it and reload my Fragment in order to apply it, like such:

 listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // Get selected language
            String selectedLanguage = newValue.toString();
            preference.setSummary(selectedLanguage);

            // Change language
            Locale locale;
            if (selectedLanguage.equals("French")) {
                locale = new Locale("fr");
            } else {
                locale = new Locale("en");
            }
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getActivity().getApplicationContext().getResources().updateConfiguration(config, null);

            // Refresh fragment
            getFragmentManager().beginTransaction()
                    .replace(R.id.content_frame, new SettingsFragment())
                    .commit();
            return true;
        }
    });

Per exemple, if French is chosen, it does apply the French language to all of my strings (defines in a strings-fr.xml file) but it does not do so for my ListPreferences summaries (even though they have their translation in the right files).

Two questions:


Solution

  • Is there a better way to load my language instead of reloading my Fragment?

    I think this would be the best way for an immediate load of the new langauage. It may not continue after Activity/Application restart though. In that case, you would need to check the language from SharedPreferences and load it in the Activity/Application onCreate():

    private Locale locale = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    
        final Configuration config = getBaseContext().getResources().getConfiguration();
        final String language = PreferenceManager.getDefaultSharedPreferences(this).getString("language", "");
        if (!TextUtils.isEmpty(language) && !config.locale.getLanguage().equals(language))
        {
            locale = new Locale(language);
            setLocale(config);
        }
    
        setContentView(R.layout.main_activity);
        ...
    }
    

    And how may I update my ListPreferences summary to the right language?

    Your question is missing XML files but here is how I usually do that and it works just fine for me every time and I don't need to update the summary programmatically:

    preferences.xml

    <ListPreference
        android:defaultValue="en"
        android:entries="@array/language_names"
        android:entryValues="@array/language_values"
        android:icon="@drawable/ic_language"
        android:key="language"
        android:summary="@string/current_language"
        android:title="@string/language_title"/>
    

    strings.xml

    <string-array name="language_names">
        <item>English</item>
        <item>French</item>
    </string-array>
    <string-array name="language_values">
        <item>en</item>
        <item>fr</item>
    </string-array>
    <string name="current_language">English</string>
    

    strings-fr.xml

    <string-array name="language_names">
        <item>Anglais</item>
        <item>Français</item>
    </string-array>
    <string name="current_language">Français</string>