In my Android application, I have a preference function that allows the user to override the language of the application. To do this, I call this function in each onResume () action, and then reset the content view:
public static void checkOverrideSystemLanguage(Context context) { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context); // Check if system language setting needs to be overridden boolean overrideSystemLanguage = prefs.getBoolean(context .getString(R.string.pref_key_chkbx_override_system_language), false); if (overrideSystemLanguage) { // Get language selection and possible languages String localeString = prefs.getString( context.getString(R.string.pref_key_list_languages), ""); List<String> possibleLanguages = Arrays.asList(context .getResources().getStringArray( R.array.pref_values_list_languages)); if (possibleLanguages.contains(localeString)) { // Change language setting in configuration Locale locale = new Locale(localeString); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); } // Use default language else { overrideSystemLanguage = false; } } // Use default language if (!overrideSystemLanguage) { context.getResources().updateConfiguration( Resources.getSystem().getConfiguration(), context.getResources().getDisplayMetrics()); } }
In every action, it works great. However, in preference activity, when the user changes the language, he is not updated immediately, because there is no setContentView () method. The user must go back to the previous action and run the settings action again to display the language change.
I tried the following in the onPreferenceChange () listener:
Update configuration, delete and read settings:
checkOverrideSystemLanguage(this);
Configuration update and call onCreate (null)
Completing and restarting an operation
finish(); startActivity(new Intent(this, PreferencesActivity.class));
Thanks for your help!
source share