Skip to content

Instantly share code, notes, and snippets.

@tiwiz
Created August 23, 2016 10:29
Show Gist options
  • Save tiwiz/59fc54de6c6b8a2beb951cacc6ee3844 to your computer and use it in GitHub Desktop.
Save tiwiz/59fc54de6c6b8a2beb951cacc6ee3844 to your computer and use it in GitHub Desktop.
Change locale of the app
private void setLocale(String language, String country) {
Locale locale = buildLocaleWith(language, country);
Resources res = InstrumentationRegistry.getContext().getResources();
Configuration config = res.getConfiguration();
setLocaleInto(config, locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
@NonNull
private Locale buildLocaleWith(String language, String country) {
Locale locale = new Locale(language, country);
Locale.setDefault(locale);
return locale;
}
@SuppressWarnings("deprecation")
private void setLocaleInto(Configuration config, Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
} else {
config.locale = locale;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment