I want my language to change dynamically and I am trying to use onConfigurationChanged but it is not being called. I have a MainActivity that creates my action bar and viewpager. The rest of my pages are Fragments. In my SettingsFragment I have a button to switch the language to French.
langChange.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View vi) {
MainActivity main = (MainActivity)getActivity();
main.langChange();
}
});
Then in my MainActivity I have
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if (locale != null){
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
public void langChange(){
if(currentLanguage == FRENCH_LANGUAGE){
locale = new Locale("en");
Locale.setDefault(locale);
Configuration c = getBaseContext().getResources().getConfiguration();
c.locale = locale;
getBaseContext().getResources().updateConfiguration(c,getBaseContext().getResources().getDisplayMetrics());
currentLanguage = "English";
}
else if(currentLanguage == ENGLISH_LANGUAGE){
locale = new Locale("fr");
Locale.setDefault(locale);
Configuration c = getBaseContext().getResources().getConfiguration();
c.locale = locale;
getBaseContext().getResources().updateConfiguration(c,getBaseContext().getResources().getDisplayMetrics());
currentLanguage = "French";
}
actionBar.setSelectedNavigationItem(actionBar.getTabCount() - 1); //This just puts it back to the settings tab
}
The onConfigurationChanged
is not being called. In my manifest I have:
<activity android:name="MainActivity" android:screenOrientation="portrait" android:configChanges="locale"></activity>
I have tried adding one or all of these options orientation|keyboardHidden|screenSize
with no success.
The reason for all of this is because I want to change the actionBar text and all the other text once I click the button. I have a separate strings file for French.
Any help would be great.