0

I have a requirement when user tap Dutch language, the language of all activities must be in Dutch and if English then it should be in English. I have below code. I am able to change language only for one Activity, not for entire application.

When user select English button next activity is displaying in English. Now when user select Russian, that activity must show in Russian language

public class MainActivity extends Activity implements OnClickListener{

private TextView txt_hello;
private Button btn_en, btn_ru, btn_fr, btn_de;
private Locale myLocale;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.txt_hello = (TextView)findViewById(R.id.txt_hello);
    this.btn_en = (Button)findViewById(R.id.btn_en);
    this.btn_ru = (Button)findViewById(R.id.btn_ru);
    this.btn_fr = (Button)findViewById(R.id.btn_fr);
    this.btn_de = (Button)findViewById(R.id.btn_de);

    this.btn_en.setOnClickListener(this);
    this.btn_ru.setOnClickListener(this);
    this.btn_fr.setOnClickListener(this);
    this.btn_de.setOnClickListener(this);

    loadLocale();
}

public void loadLocale()
{
    String langPref = "Language";
    SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    String language = prefs.getString(langPref, "");
    changeLang(language);
}

public void saveLocale(String lang)
{
    String langPref = "Language";
    SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(langPref, lang);
    editor.commit();
}

public void changeLang(String lang)
{
    if (lang.equalsIgnoreCase(""))
        return;
    myLocale = new Locale(lang);
    saveLocale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    updateTexts();
}

private void updateTexts()
{
    txt_hello.setText(R.string.hello_world);
    btn_en.setText(R.string.btn_en);
    btn_ru.setText(R.string.btn_ru);
    btn_fr.setText(R.string.btn_fr);
    btn_de.setText(R.string.btn_de);
}

@Override
public void onClick(View v) {
    String lang = "en";
    switch (v.getId()) {
    case R.id.btn_en:
        lang = "en";
        Intent english = new Intent(MainActivity.this, SamplePage.class);
        startActivity(english);
        break;
    case R.id.btn_ru:
        lang = "ru";
        Intent russia = new Intent(MainActivity.this, SamplePage_Russia.class);
        startActivity(russia);
        break;
    case R.id.btn_de:
        lang = "de";
        break;
    case R.id.btn_fr:   
        lang = "fr";
        break;          
    default:
        break;
    }
    changeLang(lang);
}

@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (myLocale != null){
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    }
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Try [this](http://stackoverflow.com/questions/2900023/change-language-programatically-in-android) – The Dreams Wind Feb 01 '16 at 06:38
  • HI @AleksandrMedvedev... I tried but no luck.. any other sample dear –  Feb 01 '16 at 06:40
  • Language selection is based by User.. If he selected English then App's language must be English event After closing App.. and if user select Dutch then it must be in Dutch until User can't make any changes in Language section. –  Feb 01 '16 at 06:42
  • I see. Is it only wrong thing with suggested approach? Thus you just need to persist user choice (e.g. in preferences) and set up resources in each launch within 'onCreate' method of your Application – The Dreams Wind Feb 01 '16 at 06:44
  • `onCreate` is a good place to put the code, but it will not work when activities are resumed from background state(a back press for example). Language setting is usually displayed in setting area of an application. When user changes the language, you can navigate user to the first screen of your app and clear activity stack to make sure that the new language is applied to all activities. – h2nghia Feb 01 '16 at 07:51

1 Answers1

1

Instead of changing each text manually or writing separate Activity for each language, you can to do it globally with more feasible. For how please refer the following links.

Supporting Different Languages

Localizing with Resources

You have to create separate values folder for each language and mention your string resource there. And change your apps local while user tap the button. Android is very smart to find the appropriate resource (values) folder for current local language. If folder not available then the resources loaded from default resource folder.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Thnxx a lot @Gunaseelan .. But any more clues.. Actually I have multiple activities.. I have to create number of String file for every language... Any Library or jar which accompish this requirement ? –  Feb 01 '16 at 06:48
  • @yogendrasrivastava, Still now I din't find any libraries for this. – Gunaseelan Feb 01 '16 at 06:50