Skip to content Skip to sidebar Skip to footer

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 Preferen

Solution 1:

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>

Post a Comment for "Update ListPreference Summary On Language Change"