0

I have an Android Fragment (I use the singe activity mutiple fragments approach) for selecting the language of the whole app (either German or English) based on the click on an image button. I have an xml file in the value folder for the strings in English and in German (see Screenshot of the folder structure).

enter image description here

Now I use a code from another app that changes the language properly. However, in my current App it just does not work. The language of the String ressources are always displayed in English altough when I choose German as the language via this Fragment.

Here is the Java file of the Fragment for Language Selection:

/*
 Fragment for selecting the language of the app via ImageButtons
 */

public class FR_Language_Selection extends Fragment implements View.OnClickListener {

    /*
    String specifying the language of the App
     */

    public static final String LANGUAGE_GERMAN = "German";
    public static final String LANGUAGE_ENGLISH = "English";
    //Set the default language to GERMAN
    public static String currentLanguageOfTheApp = LANGUAGE_GERMAN;
    
    public static boolean veryFirstCreation = true;
    public static boolean veryFirstChoice = true;

    public FR_Language_Selection() {
        // Required empty public constructor
    }


    public static FR_Language_Selection newInstance(String param1, String param2) {
        FR_Language_Selection fragment = new FR_Language_Selection();
        return fragment;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
           Set the language to "German" for the XML-layout files during the very first creation of this fragment
        */

        if (veryFirstCreation) {

            veryFirstCreation = false;
            Locale locale;
            locale = new Locale("de", "DE");

            Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
            Locale.setDefault(locale);
            config.setLocale(locale);
            getActivity().recreate();

            getActivity().getBaseContext().getResources().updateConfiguration(config,
                    getActivity().getBaseContext().getResources().getDisplayMetrics());

        }

    }


    private FragmentLanguageSelectionBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentLanguageSelectionBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.imageButtonGerman.setOnClickListener(this);
        binding.imageButtonEnglish.setOnClickListener(this);
        if(currentLanguageOfTheApp.equals(LANGUAGE_ENGLISH)) {
            binding.textViewCurrentLanguageValue.setText(LANGUAGE_ENGLISH);
            binding.imageButtonGerman.setAlpha(0.5f);
            binding.imageButtonEnglish.setAlpha(1.0f);
        }
        if(currentLanguageOfTheApp.equals(LANGUAGE_GERMAN)) {
            binding.textViewCurrentLanguageValue.setText(LANGUAGE_GERMAN);
            binding.imageButtonGerman.setAlpha(1.0f);
            binding.imageButtonEnglish.setAlpha(0.5f);
        }

    }


    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.imageButton_German) {

             /*
            Set the language to "German" for other fragments and database queries 
             */

            this.currentLanguageOfTheApp = LANGUAGE_GERMAN;


            /*
            Set the language to "German" for the XML-layout files (IMPORTANT PART)
             */

            Locale locale;
            locale = new Locale("de", "DE");

            Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
            Locale.setDefault(locale);
            config.setLocale(locale);
            getActivity().recreate();

            getActivity().getBaseContext().getResources().updateConfiguration(config,
                    getActivity().getBaseContext().getResources().getDisplayMetrics());


        }

        if(view.getId() == R.id.imageButton_English) {

            /*
            Set the language to "English" for other fragments and database queries
             */

            this.currentLanguageOfTheApp = LANGUAGE_ENGLISH;


            /*
            Set the language to "English" for the XML-layout files (IMPORTANT PART)
             */

            Locale locale;
            locale = new Locale("en", "EN");

            Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
            Locale.setDefault(locale);
            config.setLocale(locale);
            getActivity().recreate();

            getActivity().getBaseContext().getResources().updateConfiguration(config,
                    getActivity().getBaseContext().getResources().getDisplayMetrics());

        }


    }

}

I have highlighted the "IMPORTANT PART" in the code that should actually change the language of the string ressources in the XML layout files of the App (which it does in my other App but not in this one). Any idea, why the language is not changing=

VanessaF
  • 515
  • 11
  • 36
  • Try `locale = Locale("de"):` instead of ` locale = new Locale("de", "DE");` – Android Geek Jan 22 '22 at 12:59
  • @AndroidGeek: Thanks for your comment. I tried what you suggested but it yields the following error "error: cannot find symbol locale = Locale("de");" (it was also marked red by Android Studio before running). Then I tried the following `locale = new Locale("de");` The app starts but the behaviour remains unchanged. The string resources are always shown in English and not in German. – VanessaF Jan 22 '22 at 15:04
  • does it work when the device language is German? – Sambhav Khandelwal Jan 23 '22 at 06:35
  • @SambhavKhandelwal: Thanks for your comment. What do you mean with device language is German? I use an Emulator and when I try to change the language to German, it still remains in English. When using the very same emulator and code in another app, the language changes to Germand (and back to English if I want to). – VanessaF Jan 23 '22 at 09:26
  • @AndroidGeek: Any comments to my last comments? You suggested approach does not work – VanessaF Jan 26 '22 at 17:31
  • Change German languages string.xml folder from de-rDE to de – Android Geek Jan 27 '22 at 03:44
  • @AndroidGeek: Thanks for your answer Android Geek. In fact it solved the problem :-). There is still one thing that I don't understand. When I press the button for one language, the app automatically navigates to the home fragment, altough I don't have a statement for this. In my other app, using the same code, the app stays at this very fragment. Do you know why the app automatically navigates to the home fragment and how the App can stay at this fragment? – VanessaF Jan 27 '22 at 18:15
  • @VanessaF When we change language or theme of app at runtime then our activity restart again, If Activity have fragment then it automatically navigates to first fragment, We need to save state of Activity so same fragment will be open on language change. Check these answers - https://stackoverflow.com/questions/65598185/how-to-change-language-runtime-for-fragment , https://stackoverflow.com/questions/28604662/how-to-refresh-a-fragment-after-language-change-is-applied-in-android – Android Geek Jan 28 '22 at 04:23
  • @AndroidGeek: Thanks a lot Android Geek for your help. I really appreciate it (if you want to, you can wright your comments as an answer and I will upvote and accept it). – VanessaF Jan 29 '22 at 08:22
  • 1
    I'm so glad it helps you :) – Android Geek Jan 31 '22 at 03:43
  • @VanessaF I am facing same issue. When I run application from studio then language properly switch English to another language and back forth but when I build bundle and upload on play console that time language remains to English. I don't know whats wrong in the code, same code work but different behaviour. – Pratik Nov 21 '22 at 13:33
  • @Pratik: Thanks for the comment and the information. Actually I have to admit that I have never uploaded anything on play console. Do you have different xml files for the string ressources and activate the language by using `Locale locale; locale = new Locale("de", "DE"); Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration()); Locale.setDefault(locale); config.setLocale(locale); getActivity().recreate();` – VanessaF Nov 25 '22 at 16:53

0 Answers0