Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm create a simple project in android studio to practice on language changing. I do it through "choose language" button butt in running mood it's work fine, while I used shared preferences to keep change the after closing and reopen the app, but when I reopen the app default language(English) again set on the app although I change the language before closing the app. I create a login activity to practice it and add a button(choose language) on that activity to change language. Here is my code. Please help me where I am doing wrong!

What I have tried:

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setTitle(R.string.title_activity_login);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        loadLocale();
        binding.btnChangeLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeLanguage();
            }
        });
    }

    private void changeLanguage() {
        final String languages [] = {"عربي", "বাংলা", "English", "हिंदी", "اردو"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.choose_language);
        builder.setSingleChoiceItems(languages, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which==0) {
                    setLocale("ar");
                    recreate();
                }
                else if (which==1) {
                    setLocale("bn");
                    recreate();
                }
                else if (which==2) {
                    setLocale("");
                    recreate();
                }
                else if (which==3) {
                    setLocale("hi");
                    recreate();
                }
                else if (which==4) {
                    setLocale("ur");
                    recreate();
                }

            }
        });
        builder.create();
        builder.show();
    }

    private void setLocale(String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Configuration configuration = new Configuration();
        configuration.locale = locale;
        getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources()
                .getDisplayMetrics());

        SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
        editor.putString("appLanguage", language);
        editor.apply();
        Toast.makeText(this, "Language Changed", Toast.LENGTH_SHORT).show();
    }


    private void loadLocale() {
        SharedPreferences preferences = getSharedPreferences("Settings", MODE_PRIVATE);
        String language = preferences.getString("appLanguage", "");
        setLocale(language);
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900