Is it possible to change the language of an app programmatically while still using Android resources?
If not, is it possible to request a resource in an specific language?
I would like to let the user change the language of the app from the app.
Is it possible to change the language of an app programmatically while still using Android resources?
If not, is it possible to request a resource in an specific language?
I would like to let the user change the language of the app from the app.
Create a class Extends
Application
and create a static method. Then you can call this method in all activities beforesetContentView()
.Usage in activities:
Time for a due update.
First off, the deprecated list with the API in which it was deprecated:
configuration.locale
(API 17)updateConfiguration(configuration, displaymetrics)
(API 17)The thing no question answered recently has gotten right is the usage of the new method.
createConfigurationContext is the new method for updateConfiguration.
Some have used it standalone like this:
... but that doesn't work. Why? The method returns a context, which then is used to handle Strings.xml translations and other localized resources (images, layouts, whatever).
The proper usage is like this:
If you just copy-pasted that into your IDE, you may see a warning that the API requires you targeting API 17 or above. This can be worked around by putting it in a method and adding the annotation
@TargetApi(17)
But wait. What about the older API's?
You need to create another method using updateConfiguration without the TargetApi annotation.
You don't need to return a context here.
Now, managing these can be difficult. In API 17+ you need the context created (or the resources from the context created) to get the appropriate resources based on localization. How do you handle this?
Well, this is the way I do it:
This code works by having one method that makes calls to the appropriate method based on what API. This is something I have done with a lot of different deprecated calls (including Html.fromHtml). You have one method that takes in the arguments needed, which then splits it into one of two (or three or more) methods and returns the appropriate result based on API level. It is flexible as you do't have to check multiple times, the "entry" method does it for you. The entry-method here is
setLanguage
PLEASE READ THIS BEFORE USING IT
You need to use the Context returned when you get resources. Why? I have seen other answers here who use createConfigurationContext and doesn't use the context it returns. To get it to work like that, updateConfiguration has to be called. Which is deprecated. Use the context returned by the method to get resources.
Example usage:
Constructor or somewhere similar:
And then, whereever you want to get resources you do:
Using any other context will (in theory) break this.
AFAIK you still have to use an activity context to show dialogs or Toasts. for that you can use an instance of an activity (if you are outside)
And finally, use
recreate()
on the activity to refresh the content. Shortcut to not have to create an intent to refresh.For Android 7.0 Nougat (and lower) follow this article:
Change Language Programatically in Android
Old answer
This include RTL/LTR support:
I encountered the same problem: I needed to set my language to a language chosen in my app.
My fix was this:
Example:
From these XML's, you can still extract the needed strings to resources.
It's really work:
Enter your language code in
languageToLoad
variable:You can find an example here