I am doing this mobile app in Java and I want to have language selection. I managed to this in way below, but I don't want to use deprecated updateConfiguration() method, but I couldn't find working alternative
public class LanguageHandler {
public static final String ENGLISH_LANG_CODE = "en";
public static final String POLISH_LANG_CODE = "pl";
private static String currentLanguage = ENGLISH_LANG_CODE;
public static void setLanguage(@NonNull Context context, String languageCode) {
if (languageCode.equalsIgnoreCase(currentLanguage)) {
return;
}
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLocales(new android.os.LocaleList(locale));
Context updatedContext = context.createConfigurationContext(configuration);
if (context instanceof Activity && !languageCode.equalsIgnoreCase(currentLanguage)) {
currentLanguage = languageCode;
Activity activity = (Activity) context;
activity.getBaseContext().getResources().updateConfiguration(configuration, updatedContext.getResources().getDisplayMetrics());
activity.recreate();
}
}
}
This is how i use this class in my Activity
private void setUpLanguageSelector() {
RadioGroup languageSelector = findViewById(R.id.language_selector);
languageSelector.check(R.id.lang_english);
languageSelector.setOnCheckedChangeListener((radioGroup, checkedID) -> {
if(checkedID == R.id.lang_english) {
setLanguage(StartViewActivity.this, ENGLISH_LANG_CODE);
} else if (checkedID == R.id.lang_polish) {
setLanguage(StartViewActivity.this, POLISH_LANG_CODE);
}
});
}
Could someone help me with an alternative approach or suggest how I could adjust my project structure to make it work without deprecated method? If my current structure is causing the issue, I would appreciate advice on how to proceed.