john123
john123

Reputation: 589

Locale changed programmatically, but app show in System default locale on Android 2.3.3

My device system is in English.

In my AndroidManifest.xml, I defined my activity to check configuration changes:

<activity
   ...
   android:configChanges="locale" >

In my Activity, I add the function :

@Override
public void onConfigurationChanged(Configuration newConfig) {

      super.onConfigurationChanged(newConfig);



       Locale.setDefault(newConfig.locale);

       Log.v("*Locale is*", newConfig.locale.toString());

       getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

}

In my Activity onResume(), I called the above function:

@Override
public void onResume() {
    super.onResume();

     // I explicitely force my app to display in Finnish
     Configuration newConfig = new Configuration();
     newConfig.locale = new Locale("fi");
     onConfigurationChanged(newConfig);
}

(My Activity hosts fragments, Each screen view is a Fragment.)

With above code, I suppose my app will show in Finnish when launched. It works fine on Android 4.1.1.

But when I run my app on Android 2.3.3 device, the following thing happens:

Scenario 1: Launch the app from desktop ==> the app is showing in Finnish, No problem

Scenario 2: Login to my app, ==> then close the app ==> then, launch the app again from desktop ==> the app is showing in English!! Why???

(the log message Log.v("*Locale is*", newConfig.locale.toString()); shows me "fi" always!)

I verified that in Scenario 2, app always show the system default Locale when launch it again from desktop. Why?

I have no idea why in my Scenario 2, my app is showing in system locale English....any one could help?

Upvotes: 0

Views: 3348

Answers (2)

lubart
lubart

Reputation: 1838

It looks like some bug in Android 2.x

Because if you change locale not in onCreate() but in createMenu() or inside some spinner.setOnItemSelectedListener() it will work.

It is workaround but try to call some event after start your app and change locale in it.

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 8939

You can change the locale pragmatically like

public void setDefaultLocale(Context context, Locale locale) {
        Locale.setDefault(locale);
        Configuration appConfig = new Configuration();
        appConfig.locale = locale;
        context.getResources().updateConfiguration(appConfig,
                context.getResources().getDisplayMetrics());
        System.out.println("trad" + locale.getLanguage());
    } 

and call this method like

setDefaultLocale(getBaseContext(), Locale.FRENCH);

setDefaultLocale(getBaseContext(), Locale.TRADITIONAL_CHINESE);

setDefaultLocale(getBaseContext(), Locale.ITALIAN);

Its your choice to which language you want to change and start your current Activity to reflect the changes.

Intent intent = new Intent(this, YourActivity.class); startActivity(intent);

Hi John, I tested from my side to overcome your Scenario-2 and after changing the locale pragmatically and go to home screen and again launch my application its is still to my changed language ie.e Italy.

I posted code in this link

Please go through this and let me know it works for you.

Upvotes: 2

Related Questions