0

I am new to Android. I've created an app in two languages (English & Dutch). The default language is Dutch, users can change the language with an AlertDialog. I want that the users can choose English as default language with the checkbox. How can I do that?

I tried:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == R.id.action_language) {

        final String[] language =
                {
                        "Set as default language",
                };

        final boolean[] itemsChecked = new boolean[language.length];

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setIcon(R.drawable.dialogopng);
        alertDialog.setTitle("Select Language");

        alertDialog.setMultiChoiceItems(language, itemsChecked, new DialogInterface.OnMultiChoiceClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                itemsChecked[which] = isChecked;
            }
        });

        alertDialog.setPositiveButton("Dutch", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(), "Dutch", Toast.LENGTH_SHORT).show();
                setLocale("nl");
            }
        });

        alertDialog.setNegativeButton("English", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(), "English", Toast.LENGTH_SHORT).show();
                setLocale("");
            }
        });
        alertDialog.show();

        return true;
    }
    return super.onOptionsItemSelected(item);
}

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);
    Intent refresh = new Intent(this, MainActivity.class);
    startActivity(refresh);
}
Follie
  • 41
  • 1
  • 6
  • See that answer http://stackoverflow.com/questions/12230553/android-how-to-change-the-application-language-at-runtime?answertab=active#tab-top – NazarK Mar 27 '15 at 14:33

1 Answers1

0

Try this code and re-load your activity in order to take it effect,

        Locale locale = new Locale(lang); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
  • It works, but if I stop and restart the app the default language comes again and not the last selected language. Can i save the last choosed language? – Follie Mar 27 '15 at 15:33
  • sure, use shared preferences (http://www.tutorialspoint.com/android/android_shared_preferences.htm) for that, then save the changed language code in onOptionsItemSelected method, then get it at the starting activity and call above method. Also accept the answer if it it helped you. – Heshan Sandeepa Mar 27 '15 at 15:37