-2

How to change android app locale/language runtime/programmatically, best practice way?

I've looked other solutions on so but they got depreciated accepted solutions. Hope to this will be future reference.

Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
  • 1
    You can write a custom context wrapper and attach it as base context in your activity. [Here](http://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077) is a reference point. – mertsimsek Mar 10 '17 at 14:06

2 Answers2

1

I have the same issue. Here is the fix

Community
  • 1
  • 1
Maxim Petlyuk
  • 1,014
  • 14
  • 20
-2

For Android < N

Locale locale = new Locale(mLanguageCode);
Locale.setDefault(locale);
Configuration config = mContext.getResources().getConfiguration();
config.locale = locale;
mContext.getResources().updateConfiguration(
    config,
    mContext.getResources().getDisplayMetrics()
);
activity.recreate();

For >= N

Locale locale = new Locale(mLanguageCode);
Locale.setDefault(locale);
Configuration config = mContext.getResources().getConfiguration();
config.setLocale(locale);
mContext.getResources().updateConfiguration(
    config,
    mContext.getResources().getDisplayMetrics()
);
activity.recreate();
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
  • Sorry but updateConfiguration method was deprecated in API level 25. See createConfigurationContext(Configuration). https://developer.android.com/reference/android/content/res/Resources.html – Ali Karaca Mar 10 '17 at 13:58
  • You're right, I don't try yet to use this new function `createConfigurationContext()` – Kevin Robatel Mar 10 '17 at 14:49