Reputation: 129
I am currently working on an Android app that needs to support languages English and Arabic, i am using this code to switch from a language to the other.
String languageToLoad; // your language
if (languageSwitch.isChecked()) {
languageToLoad = "ar";//arabic
} else {
languageToLoad = "en";//english
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
// store it in the cache for any further use
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferencesUtil.putString(this, "language", languageToLoad);
It was working fine, i've made 2 layouts folders, a normal one and a layout-ar
. It was working correctly but sometimes it gets messed up and instead of showing the Arabic one, it shows the English layout and the app continues on running in the English mode only.
Upvotes: 1
Views: 1267
Reputation: 129
It turns out that the webview is the one responsible for the problem, once the webview is generated it deletes all the overrided local data and inserts the one from the device,in order to correct the problem you need to follow the steps found in this link.
Upvotes: 0
Reputation: 9615
At the end you should restart you activity. Try this.
public void updateActivity() {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Upvotes: 1