0

Locale is working fine for Oreo,Pie and Q versions.But for Nougat,Marshmallow and Lollipop, it is not changing the language.Any help will be highly appreciated.Thanks

   Configuration config = new Configuration();
   if (Build.VERSION.SDK_INT >= 24) {
          config.setLocale(locale);
          getBaseContext().getResources().updateConfiguration(config,
   getBaseContext().getResources().getDisplayMetrics());
           } else {
             config.setLocale(locale);
             config.setLayoutDirection(locale);
              getBaseContext().getApplicationContext().createConfigurationContext(config);
     }
cijo
  • 13
  • 1
  • 6

2 Answers2

0

You can try this

 public void changeLangs( String lang) {
    Resources res = this.getResources();
    Configuration conf = res.getConfiguration();
    conf.locale = new Locale(lang);
    res.updateConfiguration(conf, res.getDisplayMetrics());
}

then call with lang like

changeLangs("ar");
Jyotish Biswas
  • 674
  • 5
  • 11
0
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT > 24) {
     config.setLocale(locale);
     getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    } else {
         Resources resources = getResources();
                        Configuration configuration = resources.getConfiguration();
                        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
                            configuration.setLocale(locale);
                        } else{
                            configuration.locale=locale;
                        }
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
                            getApplicationContext().createConfigurationContext(configuration);
                        } else {
                            resources.updateConfiguration(configuration,displayMetrics);
                        }
                    }``
cijo
  • 13
  • 1
  • 6