1

I am making an app for a school project How can I change the Language programmatically with a save function that when the user quits the app and opens it again the language is still the language that the user has selected.

public class Settings extends Activity implements View.OnClickListener {

private Typeface ttf;
Spinner spinnerctrl;
Locale myLocale;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_einstellungen);
    //Like Button
    //Schrift & OnClickListener
    ttf = Typeface.createFromAsset(getAssets(), "schrift.ttf");
    ((TextView)findViewById(R.id.settings_title)).setTypeface(ttf);
    ((TextView)findViewById(R.id.appinfo)).setTypeface(ttf);
    findViewById(R.id.appinfo).setOnClickListener(this);
    ((TextView)findViewById(R.id.settings_back)).setTypeface(ttf);
    ((TextView)findViewById(R.id.settings_rate)).setTypeface(ttf);
    findViewById(R.id.settings_back).setOnClickListener(this);
    //Spinner Language
    spinnerctrl = (Spinner) findViewById(R.id.spinner);
    spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (pos == 1) {
                setLocale("ar");
            } else if (pos == 2) {
                setLocale("en");
            } else if (pos == 3) {
                setLocale("de");
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
}

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, Settings.class);
    startActivity(refresh);
    finish();
}

@Override
public void onBackPressed() {
    finish();
}

@Override
public void onClick(View view) {
    if(view.getId()==R.id.appinfo) {
       showAppinfo();
    } else if(view.getId()==R.id.settings_back) {
        finish();
    }
}

private void showAppinfo() {
    final Dialog dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    dialog.setContentView(R.layout.dialog_appinfo);
    ((TextView)(dialog.findViewById(R.id.appinfo_herrausgeber))).setTypeface(ttf);
    ((TextView)(dialog.findViewById(R.id.appinfo_name))).setTypeface(ttf);
    ((TextView)(dialog.findViewById(R.id.appinfo_version))).setTypeface(ttf);
    ((TextView)(dialog.findViewById(R.id.appinfo_developer))).setTypeface(ttf);
    ((TextView)(dialog.findViewById(R.id.appinfo_back))).setTypeface(ttf);
    ((TextView)(dialog.findViewById(R.id.appinfo_version_number))).setTypeface(ttf);
    dialog.findViewById(R.id.appinfo_back).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            return;
        }
    });
    dialog.show();
}
public void bewertung_internet (View view) {
    goToUrl("https://play.google.com/store/apps/details?id=de.developer.sixtysix.hiddenandroid");
}

private void goToUrl (String url) {
    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

The Code. But is in another Activity. I go in the Activity by cklicking at a button in the MainActivity so i need a script to save in the settings activity and load at start in the main activity.


Thanks. Tim

  • 1
    To load and save the language, you can use SharedPreferences. To choose from, you have to provide a list of languages in a ListPreference. To apply the choosen one, just restart the Activity. – Phantômaxx Sep 29 '15 at 15:01
  • 1
    Try to save the language selection in shared preferences. Look this to change language thru code http://stackoverflow.com/questions/2900023/change-language-programatically-in-android – Ravi Gadipudi Sep 29 '15 at 15:02
  • Thanks I will try it and say if it works! –  Sep 29 '15 at 16:16
  • I don't know how to implement the code. Can somebody help me. –  Sep 30 '15 at 18:59

1 Answers1

0

Storing the language

SharedPreferences settings = getSharedPreferences(appName,0); SharedPreferences.Editor editor = settings.edit(); editor.putString("key","lang"); editor.commit();

Obtaining the language

SharedPreferences settings = getSharedPreferences(appName,0); settings.getString("key", "defaultvalue");

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47