0

I'm changing the language from English to Arabic programmatically using this function :

private void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getContext().getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        conf.setLocale(new Locale(lang));
        Locale.setDefault(myLocale);
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(getContext(), MainActivity.class);
        getActivity().finish();
        startActivity(refresh);

    }

The language is changing,however when using Locale.getDefault().getLanguage(); or Resources.getSystem().getConfiguration().locale.getLanguage(); I'm still getting English ( en ) instead of the expected value ar although the displayed language is arabic. Any idea why it's still returning en?

Joseph Hajjar
  • 138
  • 2
  • 12

1 Answers1

0

The configuration is overwritten whenever you go through onCreate(), and a few other calls by the system. So you would need to save the locale in the SharedPreferences & set it in every onCreate() method of every activity.

See here for possible ways: Change app language programmatically in Android

B3nedikt
  • 1
  • 1