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);
}