0

My android application depends on some library that behaves wrongly on changing the language of the phone to some arabic language. So I want my app locale to remain locale.US. For that, I searched and found the following code in MainApplication:

 public class MyApplication extends Application
 {

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);

            Locale.setDefault(Locale.US);
            newConfig.locale = Locale.US;
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

    }

    @Override
    public void onCreate()
    {

            locale = Locale.US;
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    }
}

This code creating problem on android L. When I change the language of the phone to arabic, the onConfigurationChanged function is called single times , but the activity keeps on getting created after opening the app. It looks like it is creating in a loop. So,

  1. Can I use Locale.setDefault(Locale.US) without getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); as it is working correctly.
  2. Is there any other way to specify the locale of the android app.
user2436032
  • 365
  • 1
  • 4
  • 13

1 Answers1

0
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig); // call of super BEFORE setting the locale ??

        Locale.setDefault(Locale.US);
        newConfig.locale = Locale.US;
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

}

You where close... Just do it in the right order

    Locale.setDefault(Locale.US);
    newConfig.locale = Locale.US;
    super.onConfigurationChanged(newConfig);

And why are you using the "onChange" ?

Neverover
  • 31
  • 7
  • Hi @Neverover , I tried the `super.onConfigurationChanged(newConfig);` after the calculation but still that infinite `onCreate` problem is coming. The problem is coming due to line `getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());` ... So when I removed that line and keep only the `Locale.setDefault(Locale.US)` everything is working fine. But I am not sure if it works for every version of android. Also, Why every answer has it, refer: http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself, – user2436032 Sep 10 '15 at 13:52