DEV Community

Cover image for How to Dynamically Change App Language in Jetpack Compose (Modern Android Guide) πŸš€πŸŒ
Anurag Kanojiya
Anurag Kanojiya

Posted on

How to Dynamically Change App Language in Jetpack Compose (Modern Android Guide) πŸš€πŸŒ

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")
Enter fullscreen mode Exit fullscreen mode

If you're using Version Catalogs it may look like:

implementation(libs.androidx.appcompat)
Enter fullscreen mode Exit fullscreen mode

🧩 Step 2 β€” Use AppCompatActivity

To enable the locale API, your MainActivity must extend AppCompatActivity.

Replace this:

class MainActivity : ComponentActivity()
Enter fullscreen mode Exit fullscreen mode

with this:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example:

English

<string name="hello">Hello</string>
Enter fullscreen mode Exit fullscreen mode

Hindi

<string name="hello">hola</string>
Enter fullscreen mode Exit fullscreen mode

Android automatically loads the correct translation depending on the selected locale.

Language qualifiers example


🧠 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()
        )
    }
}
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

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")
)
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Or if you're showing a list of languages:

languages.forEach { language ->
    LanguageManager.setLanguage(language.code)
}
Enter fullscreen mode Exit fullscreen mode

Example button:

Button(
    onClick = {
        LanguageManager.setLanguage("hi")
    }
) {
    Text("Switch to Hindi")
}
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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

β€’ Twitter β€’ LinkedIn β€’ GitHub

Top comments (0)