Search code examples
androidlocale

Inconsistent behaviour of conf.setLocale()


As per the given solution, I'm using the following code to change the language programmatically:

       DisplayMetrics dm = res.getDisplayMetrics();
       Configuration conf = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= 17) {
            conf.setLocale(newLocale);
        } else {
            conf.locale = newLocale;
        }
       res.updateConfiguration(conf, dm);

I'm calling this in onCreate() in each activity before setContentView() and after super.Oncreate().

conf.setLocale() internally calls setLayoutDirection() and is supposed to change the layout direction according to the specified locale. But it's behaviour is not consistent. The layout direction gets overridden by the system frequently. What should be the workaround for this?

Any help would be greatly appreciated.


Solution

  • It seems that obtaining resources with Application's context were causing this inconsistent behaviour. Using getResources() from the activity instead resolves the problem.