Search code examples
androidstringandroid-fragmentsandroid-bottomnav

String resources for different languages not working in BottomNavigationBar


I use a One-Activity-Multiple-Fragments approach for my app and I have a BottomNavigationBar that is embeded in the XML layout file of the one activity. Further I have a Fragment for language selection (FR_LanguageSelection). Basically the language selection works as it should for all my fragments, unfortunately the language of the string resources of the BottomNavigationBar (android:title = "@string/Language" and android:title = "@string/Back") do not change when I choose another language in the FR_LanguageSelection, altough the resource in the string-XML-File exist.

Now I wanted to ask you whether you have a clue which the language is adjusted in every fragment that I have when choosing another language in FR_LanguageSelection while the language of the BottomNavigationBar remains the same? I'd appreciate every comment and will be quite thankful for your help.

Here you see the XML-file of the BottomNavigationBar

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:icon = "@drawable/ic_baseline_arrow_left_24"
        android:title = "@string/Back" />

    <item
        android:id="@+id/FR_LanguageSelection"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "@string/Language" />

    <item
        android:id="@+id/Fragment1"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "Fragment1" />

</menu>

Here you see the Java file of the Fragment for selecting the language FR_LanguageSelection:

package com.example.td.bapp;

import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.td.bapp.databinding.FragmentLanguageSelectionBinding;

import java.util.Locale;

/**

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

public class FR_LanguageSelection extends Fragment implements View.OnClickListener {



    /*
    String specifying the language of the App
     */
    public static String currentLanguageOfTheApp;
    public static final String LANGUAGE_GERMAN = "German";
    public static final String LANGUAGE_ENGLISH = "English";


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


    public static FR_LanguageSelection newInstance(String param1, String param2) {
        FR_LanguageSelection fragment = new FR_LanguageSelection();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    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.imageButtonGermany.setOnClickListener(this);
        binding.imageButtonUK.setOnClickListener(this);
    }


    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.imageButtonGermany) {

             /*
            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
             */

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

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

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



            Navigation.findNavController(view).navigate
                    (FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());


        }

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

            /*
            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
             */


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

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

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


            Navigation.findNavController(view).navigate(FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());
        }

        }




}

Reminder: Any idea how I can fix that problem?


Solution

  • try adding recreate()

    public void onClick(View view) {
        ...
      recreate()
    }
    

    I guess, you are updating configurations but not forcing activity to use those new ones. Calling recreate() should trigger onConfiguration change event and force activity to use new configs.