How to refresh activity after changing language (Locale) inside application
If I imagined that you set android:configChanges
in manifest.xml and create several directory for several language such as: values-fr OR values-nl
, I could suggest this code(In Activity class):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// change language by onclick a button
Configuration newConfig = new Configuration();
newConfig.locale = Locale.FRENCH;
onConfigurationChanged(newConfig);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main);
setTitle(R.string.app_name);
// Checks the active language
if (newConfig.locale == Locale.ENGLISH) {
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
} else if (newConfig.locale == Locale.FRENCH){
Toast.makeText(this, "French", Toast.LENGTH_SHORT).show();
}
}
I tested this code, It is correct.
After changing language newly created activities display with changed new language, but current activity and previously created activities which are in pause state are not updated.How to update activities ?
Pre API 11 (Honeycomb), the simplest way to make the existing activities to be displayed in new language is to restart it. In this way you don't bother to reload each resources by yourself.
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Register an OnSharedPreferenceChangeListener
, in its onShredPreferenceChanged()
, invoke restartActivity()
if language preference was changed. In my example, only the PreferenceActivity is restarted, but you should be able to restart other activities on activity resume by setting a flag.
Update (thanks @stackunderflow): As of API 11 (Honeycomb) you should use recreate()
instead of restartActivity()
.
public class PreferenceActivity extends android.preference.PreferenceActivity implements
OnSharedPreferenceChangeListener {
// ...
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("pref_language")) {
((Application) getApplication()).setLocale();
restartActivity();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onStop() {
super.onStop();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}
I have a blog post on this topic with more detail, but it's in Chinese. The full source code is on github: PreferenceActivity.java
For Android 4.2 (API 17), you need to use android:configChanges="locale|layoutDirection"
in your AndroidManifest.xml. See onConfigurationchanged is not called over jellybean(4.2.1)
Since the string resources will have already been loaded for the existing locale, the activities already opened will not automatically display using strings from the new locale. The only way to solve this is to reload all of the strings and set them again on the views. Typically, a call to setContentView(...)
will be able to cover this (depending on your Activity structure), but of course it has the side-effect of losing any view state you had.
public void onResume() {
super.onResume();
...
if (localeHasChanged) {
setContentView(R.layout.xxx);
}
...
}
You will probably not want to reload the views every single time in onResume()
, but only when the locale has changed. Checking when to update the views (i.e. localeHasChanged
) is a matter of propagating the locale change event to the previous activities. This could be done in many ways, such as using static singleton-esque state or persisting this event to storage.
You could also try to minimise the number of Activities that can be opened when you can change the locale, e.g. by having the selection be at one of the initial screens.