Search code examples
javaandroidandroid-alertdialogalertsetlocale

Alert Dialog not Change when selection


I'm trying to make multiple-choice language on my application. The language change when I selected it, but the selection not change. I use two languages, If I chose the first language (Indonesia) change, but if I chose the second language (English) the selection stays on the first choice (Indonesia), and the language change to English. This is my code.

 Button changeLang = findViewById(R.id.bahasa);
        changeLang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChangeLanguageDialog();

            }
        });

This code to show the dialog alert.

  private void showChangeLanguageDialog() {
            final String[] listItems = {"Indonesia", "English"};
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(AboutActivity.this);
            mBuilder.setTitle("Pilih Bahasa");
    
    
    
            Integer selectedPos = 0;
            if(listItems.equals("Indonesia")){
                selectedPos = 1; }
    
                    else {
                    selectedPos = 0;
                }
    
            mBuilder.setSingleChoiceItems(listItems, selectedPos, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                String selectedItem = listItems[which];
    
                   if (selectedItem.equals("Indonesia")) {
                        setLocale("in");
                        recreate();
                   } else {
                       setLocale("en");
                       recreate();
                    }
                   dialog.dismiss();
                }
            });
    
            AlertDialog mDialog = mBuilder.create();
            mDialog.show();
        }

private void setLocale(String lang) {

    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("My_Lang", lang);
    editor.apply();
}

public void loadLocale(){
    SharedPreferences preferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = preferences.getString("My_Lang","");
    setLocale(language);
}

Solution

  • It seems you're setting the language correctly, but your alert dialog is not set dependent on your saved language. Instead of calling listItems, you need to call the language through shared preferences. Try the code below:

    private void showChangeLanguageDialog() {
        final String[] listItems = {"Indonesia", "English"};
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
        mBuilder.setTitle("Pilih Bahasa");
    
        String language = getSharedPreferences("Settings", Activity.MODE_PRIVATE).getString("My_Lang","");
    
        Integer selectedPos = 0;
    
        if(language.equals("en")){
            selectedPos = 1; }
        else {
            selectedPos = 0;
        }
    
        mBuilder.setSingleChoiceItems(listItems, selectedPos, (dialog, which) -> {
            String selectedItem = listItems[which];
    
            if (selectedItem.equals("Indonesia")) {
                setLocale("in");
                recreate();
            } else {
                setLocale("en");
                recreate();
            }
            dialog.dismiss();
        });
    
        AlertDialog mDialog = mBuilder.create();
        mDialog.show();
    }