How to Refresh Activity After Changing Language (Locale) Inside Application

How to refresh activity after changing language (Locale) inside application


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

Change language for whole Android application activities from Setting activity

Try this code to change the language...

                    Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
settings.edit().putString("locale", "ar").commit();
this.finish();
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);

And in each activity's onCreate and Onresume Methods you have to do somthing like this..

if (settings.getString("locale","").equals("en")) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
settings.edit().putString("locale", "en").commit();
}

Changing locale: Force activity to reload resources?

In your AndroidManifest.xml, add this attribute to your Activity

android:configChanges="locale"

In your activity override onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}

https://developer.android.com/guide/topics/manifest/activity-element.html#config

How to refresh PreferenceActivity (which launched from TabHost) after changing language (Locale) inside android-application

If your application don't have too much preferences, you can always get them one by one and reset their text. When you set the text again, it automatically find the right text with new locale

Another option is to restart preferences activity. You don't have to restart the whole app. Just open the preferences activity again and finish() the old preference activity instance.

Change display language at runtime and recreate all activities

Add "locale" property in your android:configChanges in all you activities and then override onConfigurationChanged() in activities and handle the language changes accordingly.

More on http://developer.android.com/guide/topics/manifest/activity-element.html#config



Related Topics



Leave a reply



Submit