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).
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=