Android update language in preference mode

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); // Remove all preferences and add them to update the language getPreferenceScreen().removeAll(); addPreferencesFromResource(R.xml.preferences); 
  • Configuration update and call onCreate (null)

  • Completing and restarting an operation

     finish(); startActivity(new Intent(this, PreferencesActivity.class)); 

Thanks for your help!

+6
source share
1 answer

I had a similar problem, I solved it this way (as you said: shutting down and restarting activity). He works!

  public class OptionsActivity extends PreferenceActivity implements YesNoDialogPreference.YesNoDialogListener, SharedPreferences.OnSharedPreferenceChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Options Setting final SharedPreferences prefs = getSharedPreferences(OptionsUtility.PREFERENCE_NAME, MODE_PRIVATE); String language = prefs.getString(OptionsUtility.PREFERENCE_LANGUAGE, OptionsUtility.DEFAULT_LANGUAGE); //Update locale OptionsUtility.updateLanguage(this, language); prefs.registerOnSharedPreferenceChangeListener(this); getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE); getPreferenceManager().setSharedPreferencesName(OptionsUtility.PREFERENCE_NAME); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); Preference eraseGameButton = getPreferenceManager().findPreference(OptionsUtility.PREFERENCE_ERASE_GAME); if (eraseGameButton != null) { YesNoDialogPreference yesNo = (YesNoDialogPreference)eraseGameButton; yesNo.setListener(this); } Preference configureKeyboardPref = getPreferenceManager().findPreference("keyconfig"); if (configureKeyboardPref != null) { //KeyboardConfigDialogPreference config = (KeyboardConfigDialogPreference)configureKeyboardPref; //config.setPrefs(getSharedPreferences(MemodroidCoreActivity.PREFERENCE_NAME, MODE_PRIVATE)); //config.setContext(this); } } public void onDialogClosed(boolean positiveResult) { if (positiveResult) { DatabaseScoreManager dbsManager = new DatabaseScoreManager(getApplicationContext()); dbsManager.resetHighScores(); Toast.makeText(this, R.string.toast_highScoresReset,Toast.LENGTH_SHORT).show(); } } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) { restartActivity(); } @Override protected void onStop() { super.onStop(); getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); } private void restartActivity() { Intent intent = getIntent(); finish(); startActivity(intent); } @Override public void onBackPressed() { finish(); } } 
+1
source

Source: https://habr.com/ru/post/899472/


All Articles