Setting the Language application in Android settings

I would like the application language to be set in accordance with the user settings, but so far it does not work as I would like.

I set the default values: strings.xml, as well as es-values ​​with strings.xml inside in Spanish. I have a menu item that leads the user to Preference activity, where he can amon gother things chosen language.

So here are some excerpts from the code:

public class Preference extends PreferenceActivity implements OnSharedPreferenceChangeListener { ...... // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); ...} //(......) //and here I have the listener so when the language pref changes value the locale gets changed. @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("listPref2")) { String idioma = sharedPreferences.getString("listPref2", "catala"); if ("castella".equals(idioma)) { idioma = "es_ES"; Locale locale = new Locale(idioma); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } } } 

Therefore, when I change the language, it works, but then, when I return later or restart the emulator, the language will return to the default language, en_US and the application language will again change to the default value. What can I do to deal with this?

I know that I can get this preference (which I can access from all my actions) and then set the language standard each time, but I find it a little heavy, there is no way to do this in a more elegant way ?

What I would like to do is if the user sets the language, so when he returns in 2 days, he should not change the language again.

Any ideas?

+6
source share
2 answers

OK, that might help someone. I added the following to the main action manifest:

android:configChanges="locale"

Then, when the user selects the settings, I added a confirmation button, and then this button will lead you to the main activity, so lnagages gets reset.

I have a static class where I have this code to change the locale:

 public static void updateLanguage(Context context, String idioma) { if (!"".equals(idioma)) { if ("castella".equals(idioma)) { idioma = "es_ES"; } else if ("catala".equals(idioma)) { idioma = "ca_ES"; } Locale locale = new Locale(idioma); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getResources().updateConfiguration(config, null); } } 

end with every action I have 20 of them I call this method before:

setContentView(R.layout.list_event);

Using these methods, when I rotate the screen, the actions do not change the language, here is the link to the blog that helped me: http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx

+11
source

I would think that you need to set the locale in the MainActivity onCreate method. Similarly, you set it with the onSharedPreferenceChanged method.

+1
source

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


All Articles