Ad
Localization With Strings.xml
I want to change the locale within my app. I have several strings.xml
file depending on various locales.Issue I am facing is the app locale gets updated but the strings like in title or tabs header doesn't gets updated depending on locale changed.
I have killed my app using System.exit(0)
and manually restarted it but changes doesn't gets reflected. Please help.
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
res.updateConfiguration(config, res.getDisplayMetrics());
Ad
Answer
You can use bellow code to change locale
public void setLocale(Context context, String language)
{
Locale newLocale;
switch (language)
{
case "UK":
newLocale = Locale.UK;
break;
default:
newLocale = Locale.US;
break;
}
Locale.setDefault(newLocale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(newLocale);
} else {
config.locale = newLocale;
}
context.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
Note: You must use this code on create of MainActivity.
To restart your App you can use this:
public void resetApp(Context context) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(getBaseContext().
getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad