Skip to content

Instantly share code, notes, and snippets.

@truongngoclinh
Last active July 13, 2018 06:40
Show Gist options
  • Save truongngoclinh/11441eaf7354e5f40d2ef98ef7bddd7e to your computer and use it in GitHub Desktop.
Save truongngoclinh/11441eaf7354e5f40d2ef98ef7bddd7e to your computer and use it in GitHub Desktop.
Android N + language problem
@Override
protected void attachBaseContext(Context newBase) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
super.attachBaseContext(wrap(newBase, new LocalState().getLocale().getLanguage()));
} else {
super.attachBaseContext(newBase);
}
}
public static ContextWrapper wrap(Context context, String language) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
Locale newLocale = new Locale(language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment