Last active
July 13, 2018 06:40
-
-
Save truongngoclinh/11441eaf7354e5f40d2ef98ef7bddd7e to your computer and use it in GitHub Desktop.
Android N + language problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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