Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the locale at runtime?

We have an Android app that functions as a client for a remote PC program. We'd like to add a feature so the PC can instruct the Android app to change its locale at runtime, i.e., start the app; put it in communication with the PC; sometime later the PC tells the app to switch to, say, Spanish or Chinese.

We already have all the layout and string resources set up for their respective locales. Our app is the only app the user sees so it doesn't matter if the rest of the device stays in English.

There is another thread on this at Change language programmatically in Android but it doesn't seem to reach a conclusion.

I can put . . .

Locale locale = new Locale(sTheNewLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

. . . in onCreate() before the setContentView() but that doesn't really help if I want to change the locale after my screen is up and running. Is there a way to REload the content view after the Activity is already running? So is there any practical way to change locales on-the-fly reliably or do I have to tell my boss it can't be done except for setting the whole device to the new locale before starting the app?

like image 662
user316117 Avatar asked Nov 01 '12 16:11

user316117


2 Answers

Since API 11 you can use recreate so can make this method in your activity:

private void restartInLocale(Locale locale)
{
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    Resources resources = getResources();
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    recreate();
}
like image 144
weston Avatar answered Sep 29 '22 18:09

weston


You can start a new instance of your activity and exit the old one. Here is a full (untested) example how you can store any desired language and restart your app. You just need to call restartInLanguage with your prefered language.

public class YourMainActivity extends Activity {
    private static final String APP_SHARED_PREFS = "com.example.test";
    private SharedPreferences settings;
    private Editor editor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings=getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        editor=settings.edit();

        Locale locale = new Locale(settings.getString("lang", "default-lang"));
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());

        // your stuff...
    }

    public void restartInLanguage(String lang) {
        editor.putString("lang", lang);
        editor.commit();
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    // ...
}
like image 38
rekire Avatar answered Sep 29 '22 18:09

rekire