Created
August 23, 2016 10:29
-
-
Save tiwiz/59fc54de6c6b8a2beb951cacc6ee3844 to your computer and use it in GitHub Desktop.
Change locale of the app
This file contains hidden or 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
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