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/ApplicationonCreate():

privateLocalelocale=null;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    finalConfigurationconfig= getBaseContext().getResources().getConfiguration();
    finalStringlanguage= PreferenceManager.getDefaultSharedPreferences(this).getString("language", "");
    if (!TextUtils.isEmpty(language) && !config.locale.getLanguage().equals(language))
    {
        locale = newLocale(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-arrayname="language_names"><item>English</item><item>French</item></string-array><string-arrayname="language_values"><item>en</item><item>fr</item></string-array><stringname="current_language">English</string>

strings-fr.xml

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

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