简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM