0

I've tried testing this on API 27 and it works perfectly, but when I switched to API 21 it wouldn't change the language.

private void setLocale(String lang) {

        Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();

  //config.locale = locale;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){

        config.setLocale(locale);

    } else {

        config.locale = locale;

    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){

        getBaseContext().createConfigurationContext(config);

    } else {

        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

 //getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();

}


public void loadlocale(){

    SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = prefs.getString("My_Lang", "");
    setLocale(language);
}

I call "loadlocal()" before the setContentView in the onCreate.

And in an onClickListener I execute

    ....
    setLocale("en");
    recreate();
//or
    setLocale("ar");
    recreate();
    ....

0 Answers0