Change Language in Android Programmatically
I found some interesting issue when developing an android app that supports multiple languages.
My approach was obsolete, now android has provide official API to change language programmatically https://developer.android.com/about/versions/13/features/app-languages use that instead
The sample playground can be found in here: https://github.com/wisnukurniawan/changelanguage
For the first try, I created a sample project to do playground. Inside the sample project, I made a function that set context locale based on device language.
Then I call the function in my activity like this.
Then I run and test. The result shows my text not translated all 🤐:
- Jelly Bean *worked
- Kitkat *worked
- Lollipop *worked
- Pie *not worked
On Android Pie, there is some text not translated. For a case when set text on TextView
use string from applicationContext
.
applicationContext.getString(R.string.my_string)
This issue, I simply fix it by calling LocalizationUtil.applyLanguage(newBase, “id”)
in Application
level. So my function will look like this.
Then I retested it and the issue was solved 🎉 🎉 🎉.