2

I have a requirement to change my application language from English to Hindi.I am not able to do so in Android 8.0

public static void setUpLanguage(SqrrlApplication application, String languageIsoCode) {
        try {
            Locale locale = new Locale(languageIsoCode);
            Locale.setDefault(locale);
            Resources res = application.getResources();
            Configuration config = new Configuration(res.getConfiguration());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(locale);
                application.getApplicationContext().createConfigurationContext(config);
            }else{
                config.locale = locale;
            }
            res.updateConfiguration(config, res.getDisplayMetrics());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Please Note: However, if I change my device language from device settings, my app changes to hindi version.

I have 'values-hi' folder in res having the hindi strings.

Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manish Agrawal
  • 551
  • 2
  • 6
  • 22
  • 4
    Possible duplicate of [How to change Android O / Oreo / api 26 app language](https://stackoverflow.com/questions/47165311/how-to-change-android-o-oreo-api-26-app-language) and this also https://stackoverflow.com/questions/39705739/android-n-change-language-programmatically – AskNilesh Mar 16 '18 at 10:01
  • does you locale gets changed when app is restarted ? – Gautam Mar 16 '18 at 10:01
  • @Gautam No, it doesn't affect when i restart the app. – Manish Agrawal Mar 16 '18 at 10:06
  • even i am using the same code to update locale, but all of my activities extends a BaseActivity and overrides attachBaseContext method. Inside this i call super.attachBaseContext({By calling setUpLanguage method which returns context that is passed initially}) . – Gautam Mar 16 '18 at 10:15
  • Do i need to use attachBaseContext method ? – Manish Agrawal Mar 16 '18 at 10:27
  • yes try overriding it inside activity – Gautam Mar 16 '18 at 10:55

0 Answers0