Android Switch languages within the application without restarting

5.2k views Asked by At

From the fragment (settings) by pressing the TextView called DialogFragment, in which to change the settings (language) application, how do I close by DialogFragment, apply the settings without restarting the entire application?

Ideally to change languages at once on SingleChoice selection dialog. With reboot everything works. Here is the code DialogFragment:

public class LanguageDialogFragment extends DialogFragment {

    final String[] itemsLang = {"English", "Russian", "Ukraine"};
    final String[] items = {"en", "ru", "uk"};
    String lang;
    MainActivity activity = new MainActivity();

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Выберите свой родной язык")
                .setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        lang = items[item];

                    }
                })

                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        App.prefs.saveLanguage(lang);
                        App.changeLang(lang);

                        reload();

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

        return builder.create();
    }

    private void reload() {
        Intent intent = getActivity().getIntent();
        getActivity().finish();
        Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(App.context.getPackageName());
        startActivity(LaunchIntent);
    }
}

Below are examples of the application as much as I wanted to implement: https://play.google.com/store/apps/details?id=com.funeasylearn.english

Screenshots from it:

enter image description here enter image description here

1

There are 1 answers

3
Quentin Golsteyn On

Try the code below. You have to create a new locale based on the language the user selected, set it as the current locale and restart your activity. Be aware you might have to restart other activities if they are active when you do the locale switch.

public class LanguageDialogFragment extends DialogFragment {

    private final String[] itemsLang = {"English", "Russian", "Ukraine"};
    private final String[] items = {"en", "ru", "uk"};
    private String lang;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Выберите свой родной язык")
            .setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    lang = items[item];

                }
            }).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //When user submits, restart the activity in
                    //the new language
                    restartActivityInLanguage(lang);

                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //Do nothing
                }
            });

        return builder.create();
    }

    private void restartActivityInLanguage(String language) {
        Locale locale = new Locale(language);
        Configuration config = new Configuration();
        config.locale = locale;
        Resources resources = getResources();
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        getActivity().recreate();
    }
}