I'm making multi language for my android project. Everything is perfect. Except when I close the application and start it again, it turns back to the previous language. For Example: I have 2 languages Vietnamese and English. When I start it's Vietnamese. Then I choose English. But when I close and start it again it turns back to Vietnamese?
My code:
public class setting extends Fragment {
/**
* @param args
*/
private Spinner spinnerctrl;
private Locale myLocale;
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View settingView = inflater.inflate(R.layout.setting, container, false);
spinnerctrl = (Spinner) settingView.findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (arg2 == 1) {
Toast.makeText(arg0.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
setLocale("en");
} else if (arg2 == 2) {
Toast.makeText(arg0.getContext(), "You have selected VietNam", Toast.LENGTH_SHORT).show();
setLocale("vi");
}
}
private void setLocale(String lang) {
// TODO Auto-generated method stub
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(getActivity(), MainActivity.class);
startActivity(refresh);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return settingView;
}