Many apps today let users choose their preferred language directly inside the app β not just rely on the device language.
For example:
- Someone learning a language may want the app in Spanish πͺπΈ
- Or you might simply want to test translations during development
Good news: modern Android provides a clean official way to change app language dynamically using AppCompat 1.6+.
Let's implement it step by step. π
π Step 1 β Add AppCompat
First, you need the AppCompat library because the locale API lives there.
Add this dependency:
implementation("androidx.appcompat:appcompat:1.6.1")
If you're using Version Catalogs it may look like:
implementation(libs.androidx.appcompat)
π§© Step 2 β Use AppCompatActivity
To enable the locale API, your MainActivity must extend AppCompatActivity.
Replace this:
class MainActivity : ComponentActivity()
with this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
That's it β now your app can use AppCompat locale management.
π Step 3 β Add Language Resources
Now we define localized strings.
Inside the res folder, create resource directories using language qualifiers like:
values/strings.xml
values-hi/strings.xml
values-bn/strings.xml
values-ta/strings.xml
Example:
English
<string name="hello">Hello</string>
Hindi
<string name="hello">hola</string>
Android automatically loads the correct translation depending on the selected locale.
π§ Step 4 β Create a Language Manager
Instead of repeating locale logic everywhere, we create a small LanguageManager helper.
object LanguageManager {
fun setLanguage(code: String) {
val locale = LocaleListCompat.forLanguageTags(code)
AppCompatDelegate.setApplicationLocales(locale)
}
fun resetToSystem() {
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.getEmptyLocaleList()
)
}
}
What this does
setApplicationLocales() tells Android:
"Hey, switch the entire app to this locale."
Android then refreshes the UI automatically.
Super clean β¨
π¦ Step 5 β Create a Language Model
Next, let's define a simple data model.
data class AppLanguage(
val code: String,
val label: String
)
Now define the languages supported by your app:
val languages = listOf(
AppLanguage("en", "English"),
AppLanguage("hi", "Hindi"),
AppLanguage("as", "Assamese"),
AppLanguage("bn", "Bengali"),
AppLanguage("gu", "Gujarati"),
AppLanguage("kn", "Kannada"),
AppLanguage("ml", "Malayalam"),
AppLanguage("mr", "Marathi"),
AppLanguage("or", "Odia"),
AppLanguage("pa", "Punjabi"),
AppLanguage("ta", "Tamil"),
AppLanguage("te", "Telugu"),
AppLanguage("ur", "Urdu")
)
These language codes match the resource folder qualifiers (values-hi, values-es, etc.).
π― Step 6 β Change Language from UI
Now the fun part.
You can change the language from onboarding, settings, or anywhere in your app.
Example:
LanguageManager.setLanguage("hi")
Or if you're showing a list of languages:
languages.forEach { language ->
LanguageManager.setLanguage(language.code)
}
Example button:
Button(
onClick = {
LanguageManager.setLanguage("hi")
}
) {
Text("Switch to Hindi")
}
Once triggered, Android will:
1οΈβ£ Change the locale
2οΈβ£ Reload resources
3οΈβ£ Update UI automatically
No manual activity restart required π
π Reset Language to System Default
If the user wants the app to follow the device language, just call:
LanguageManager.resetToSystem()
Android will switch back to the system locale.
β‘ Why This Approach Is Great
Using AppCompatDelegate.setApplicationLocales() gives you:
β
Official AndroidX solution
β
Works with Jetpack Compose + Views
β
No deprecated locale hacks
β
Automatic UI refresh
β
Clean and maintainable code
π Final Result
Your app now supports:
π Dynamic language switching
β‘ Instant UI updates
π± System language fallback
π§© Clean architecture
All with just a few lines of code.
π‘ Pro Tip
If you want the language to persist across app restarts, store the selected language in:
- DataStore
Then apply it when the app launches.
That's it!
Your Android app is now ready for global users π
π§βπ» Author
Anurag Kanojiya β Android app Developer | Kotlin | KMP | Java | Spring Boot

Top comments (0)