This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Changing Locale within the app itself
in my application I need to "force" language let's say that I have locale in english as default polish and finnish, according to that post I had created function posted also bellow, function is called in createActivity(), but the problem is it does not work.... any idea why? Any suggestions?
private void setLocale(String localeCode){
Locale locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
Here is what I got so far. I know this question is resolved, but my solution is easier and more compact. No other changes are needed, no android:configurationChanges attribute for all activities in your manifest.
public class SigmaMiddleEastApplication extends PPGApplication {
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.locale = Locale.ENGLISH;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
}
BEWARE this may cause problems:
What could cause an Android activity to relaunch itself infinitely when returning from camera?
Add the following in the manifest (for every activity) :
android:configChanges="locale"
Related
I'm developing an application for android and when everything should be finished I've found an extrange behaviour. In some devices the screen starts to flicker, and there is no apparent reason for that. It may happen in initial splash with only an AsyncTask quering a webservice or in the home screen with no asynctask at all.
It's strange because only happens in devices with android 4.2, nor in 2.3 or 2.2. I've tried enabling the tag harware-accelration in manifest but I've no clue for what can be the reason
Some help?
Thanks in advance.
Finally I've managed how to avoid this. Here was the trick, just changing my Application's Override OnConfigurationChanged.
This was my old code
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Locale l = getLocale();
Configuration config = new Configuration(newConfig); // get Modifiable Config from actual config changed
config.locale = l;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
and I've changed to this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Locale l = getLocale();
Configuration config = new Configuration(newConfig); // get Modifiable Config from actual config changed
config.locale = l;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
I hope someone finds this useful.
I have implemented custom locale selection about a year ago but after 4.1 release users start to complain on constant activity blinking. Here is code I'm using (compiled from different SO answers):
public final class TestApplication extends Application
{
private Locale desiredLocale = new Locale("ru-RU");
#Override
public void onCreate() {
super.onCreate();
updateLocale(new Configuration());
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
updateLocale(newConfig);
}
private void updateLocale(Configuration newConfig) {
newConfig.locale = desiredLocale;
Locale.setDefault(desiredLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
Application contains only one empty activity, which is recreated by Android every second after I change device orientation. Here is the source of sample.
It looks like all applications which use this technique became invalid. What is the correct approach?
This line caused solution to fail:
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
Correct (at least it works) implementation is defined here
https://stackoverflow.com/a/14010044/554336 :
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
So new configuration instance should be created every time.
Is it possible to translate android application at runtime using XML received from service?
If it is possible could someone please point me in right direction.
Thank you.
Warning
Everything I have read says that its not a good idea to let your app change the language because it isn't supported by the Android framework and it may cause problems.
With that being said, I have done it in my app and, while it has been a bit of a pain, it seems to be working so far. Here is how I did it in case you want to do it this way. You need a separate strings.xml file for each language. strings.xml in values folder as your default then maybe a strings.xml in say a values-es folder for Spanish strings. I have used the following code to change the configuration settings depending on a RadioButton that the user selects
final Configuration LANG_CONFIG = ChooseLocale.this.getResources().getConfiguration();
Locale newLocale = new Locale("English");
curLang = ChooseLocale.this.getLanguage();
if ((curLang.equals("English")) || (curLang.equalsIgnoreCase("Ingles")))
{
newLocale = new Locale("en_US");
}
else
{
newLocale = new Locale("es");
}
Toast.makeText(getApplicationContext(), newLangToast + " " + curLang , Toast.LENGTH_SHORT).show();
Configuration config = getBaseContext().getResources().getConfiguration();
Locale.setDefault(newLocale);
config.locale = newLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = langPref.edit();
editor.putString(LANG_PREF, curLang);
editor.commit();
With this line being the most important to update the config
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I get the locale from my getLanguage() function which can be handled however you want. I also had to add
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig = Globals.getUserLanguage(this);
super.onConfigurationChanged(newConfig);
to every activity that allows orientation change and add this to my onCreate() of each
final SharedPreferences langPref = getSharedPreferences (LANG_PREF, 0);
if (Globals.langConfig != null)
this.onConfigurationChanged(Globals.langConfig);
I also added android:configChanges="orientation|locale" to each activity in the manifest that allows orientation change
Android Docs on localization
I would like the application language to be set according to the user preferences but up till now it doesn't work how I would like it to.
I have set the default values: strings.xml and also values-es with a strings.xml inside in spanish. I have a menu option which brings the user to a Preference activity where he can amon gother things chose the language.
So here are some extracts of 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());
}
}
}
So when I change the language it works but then when I come back later or restart the emulator the language gets back to default locale the en_US and the app language gets changed back to default again. What can I do to sort that out?
I know I can get this preference (which I can access to from all my activities) and then each time set up the locale but I find it a bit heavy isn't there a way to do it in a more elegant way?
What I would like to do is if the user sets up the language so when he comes back 2 days later he doesn't have to change the language again.
Any ideas?
OK it may help someone. I have added the folowing to the main activity manifest:
android:configChanges="locale"
Then when the user choses the preferences I have put a confirm button and then this button brings you to main activity that is why the 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 at every activity I have like 20 of them I call this method before:
setContentView(R.layout.list_event);
With these methods when I rotate the screen the activities don't change the language
here is a link to a blog that helped me:
http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx
I would think that you need to be setting the locale in the MainActivity onCreate method. The same way you are setting it when the onSharedPreferenceChanged method.
I want to rotate my phone and keep the locale. I have folders values, values-en and values-hr. In every activity I have
android:configChanges="keyboardHidden|orientation|locale"
and in every activity.java file I have
private Locale locale = null;
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
and in onCreate
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString("language","en");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
It works till I rotate my phone - app is still working, buttons and everything is fine - but locale is getting weird - sometimes it's my locale (hr) sometimes it's (en)...
What else I need to add?
I simply removed "if's" and everything is working now. Yes, it is a little "bulky" every time to "re-force" a locale but I couldn't find another way.
My solution is a little bit different, I just "re-force" it on onCreate and onConfigChange - not every 100ms or so and on Desire (2.2) it's working flawlessly.
#Austyn
Yes, it's pretty much the same question but I though is there another solution except "100ms forcing".
#Mayra
Users wanna have their mother-tongue (HR) but it's not supported in (current) mobile phones so this is the only way - to force it =/