Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: reflect UI language change on the on-the-fly without reload/restarting app

I have a setting in my app that allows user to select different localization (language), ie Chinese, German, etc.

What i would like to do is that once the user makes their choice, to immediately update the layout with strings in the currently selected language. Of course, i want the lang change propagated to ALL current activities, without reloading the app.

I found this (havent tried yet), but was wondering if there is a cleaner way of doing it.

http://www.tutorialforandroid.com/2009/01/force-localize-application-on-android.html

Gracias

like image 301
Saideira Avatar asked Sep 30 '10 16:09

Saideira


1 Answers

I also had this issue.I used the code below and then it changed the language without refreshing the activity

public void setLocale(String lang) {

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    onConfigurationChanged(conf);
    /*Intent refresh = new Intent(this, AndroidLocalize.class);
    startActivity(refresh);*/
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
    lblLang.setText(R.string.langselection);
  super.onConfigurationChanged(newConfig);
}

I hope it would help you.......

like image 74
Milan Shukla Avatar answered Nov 14 '22 10:11

Milan Shukla