Reputation: 57
I have added language switching feature in my android studio application. This feature works well.
But when I restart the application, it doesn't start in the selected language. It works according to the language of the phone. I could not find how to make the selected settings remembered.
Thank you.
Here is my code:
Spinner spinner;
Locale myLocale;
String currentLanguage = "tr", currentLang;
int flags[] = {R.drawable.logo, R.drawable.turkey, R.drawable.united, R.drawable.russia};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui_settings);
currentLanguage = getIntent().getStringExtra(currentLang);
spinner = (Spinner) findViewById(R.id.spinner);
final List<String> list = new ArrayList<String>();
list.add(getString(R.string.selectlang));
list.add("Türkçe");
list.add("English");
list.add("Pусский");
final CustomAdapter adapter = new CustomAdapter(getApplicationContext(),flags, (ArrayList<String>) list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 1:
setLocale("tr");
break;
case 2:
setLocale("en");
break;
case 3:
setLocale("ru");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void setLocale(String localeName) {
if (!localeName.equals(currentLanguage)) {
myLocale = new Locale(localeName);
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);
refresh.putExtra(currentLang, localeName);
startActivity(refresh);
} else { }
}
CustomAdapter.java;
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
List<String> list = new ArrayList<String>();
public CustomAdapter(Context applicationContext, int[] flags, ArrayList<String> list) {
this.context = applicationContext;
this.flags = flags;
this.list = list;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return flags.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.customspin, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(list.get(i));
return view;
}
}
Upvotes: 1
Views: 204
Reputation: 1497
Use the following snippet
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Locale locale = new Locale("tr"); // get this value from shared pref
Locale.setDefault(locale);
Resources resources = activity.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
setContentView(R.layout.activity_ui_settings);
currentLanguage = getIntent().getStringExtra(currentLang);
spinner = (Spinner) findViewById(R.id.spinner);
final List<String> list = new ArrayList<String>();
list.add(getString(R.string.selectlang));
list.add("Türkçe");
list.add("English");
list.add("Pусский");
final CustomAdapter adapter = new CustomAdapter(getApplicationContext(),flags, (ArrayList<String>) list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 1:
setLocale("tr");
break;
case 2:
setLocale("en");
break;
case 3:
setLocale("ru");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
This will solve your problem.
Upvotes: 2