Dwayne T
Dwayne T

Reputation: 105

Language not changing for items in listView (Android)

I have an app that uses 2 languages. When i press the button to change language, it changes for the strings in the menu and navigation drawer but does not changes for items in the listView. Here is my code.

 private void showChangelanguageDialog() {
    //array of languages to display
    final String [] languages = {"English","Shona"};
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle(getString(R.string.choose_language));
    builder.setSingleChoiceItems(languages, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == 0){
                //english
                setLocale("en");
                recreate();
            }
            if (which == 1){
                //shona
                setLocale("sn");
                recreate();
            }
            //dismiss alert dialog when language is selected
            dialog.dismiss();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration configuration = new Configuration();
    configuration.locale=locale;
    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
    //save data to shared preferences
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang",lang);
    editor.apply();
}

//load saved lanuage in saved preferences
public void loadLocale(){
    SharedPreferences prefs = getSharedPreferences("Settings",Activity.MODE_PRIVATE);
    String language = prefs.getString("My_Lang","");
    setLocale(language);
}

What could be the problem?

Upvotes: 2

Views: 510

Answers (3)

Tinovimba Mawoyo
Tinovimba Mawoyo

Reputation: 410

Try compiling the app as an apk if you are compiling it as an aab. I had the same problem and it was resolved by using an apk before uploading it to playstore

Upvotes: 1

Noah Reh
Noah Reh

Reputation: 21

An easy solution for your problem should be to restart your Activty as shown here

Upvotes: 0

Sean
Sean

Reputation: 5266

You probably need to call notifyDataSetChanged() on your list to force it to refresh the views within

Upvotes: 1

Related Questions