I have a Spinner and when I click, for example, to German the App is translated to German. When I restart the App I have the default Language again. My question now is how to save the Language that when I restart the app is still changed in the selected Language?
The code of my Activity is:
private Typeface ttf;
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_einstellungen);
//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);
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");
} else if (pos == 3) {
setLocale("nv-rUs");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
//TODO Language translation System
//LanguageTranslation
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() {
}
@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);
dialog.findViewById(R.id.appinfo_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
return;
}
});
dialog.show();
}
}