Sytze Sytze - 5 months ago 28
Java Question

Android Studio: Switching string values in resources programmatically, without changing locale

For a mobile application I'm making in Android Studio, I want to make it possible for users to change the language of the application. The idea is as follows: I have multiple strings.xml files (for the different languages) and I want to switch to a specific file when the language is changed (e.g. if language is changed to German, switch to file strings.xml (de)). However, I can't seem to find a way to programmatically switch to another strings.xml file. The only solution I can find online is to change the locale of the system, but I would rather avoid this option. Is there any way to programmatically change the strings.xml file, without changing the locale?

Thanks in advance!

Answer

I would suggest using the Android-LocalizationActivity library.

This library makes it very simple to change the language of your app at runtime. All your activities need to implement the LocalizationActivity, which extends AppCompatActivity. After that you can switch the language used in your app by calling setLanguage("en"); A sample project including the sample code below and more information can be found at the github page.

this question about programmatically changing the language might contain some useful answers as well.