QuickAnswer
by

Android application development, switch display language within the application

Android application development, switch display language within the application

How to switch the display language within the app in Android app development.
For example, if you want to display English on the device even in a Japanese environment.

Android Studio Version 4.1.2
Kotlin 1.4.31

Part of the code

class MainActivity : AppCompatActivity() {
    private var localeLanguage: String = ""        // "" or "en" or "jp" or etc.

    companion object {
        internal const val SETTINGS: String = "settings"
        internal const val LOCALE_LANGUAGE: String = "localeLanguage"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    //Save localeLanguage
    //Save a string such as "en" or "ja" with some action
    //You may restart Activity by executing recreate() at the timing of saving.
    private fun saveLocaleLanguage() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putString(LOCALE_LANGUAGE, this@MainActivity.localeLanguage)
            apply()
        }
    }

    //Language setting
    //Switch the language when the Activity starts.
    //Prepare as many strings.xml as you need.
    override fun attachBaseContext(base: Context) {
        val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
        val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
        if (loc != null) {
            val res = base.resources
            val config = Configuration(res.configuration)
            val localeList = LocaleList(loc)
            LocaleList.setDefault(localeList)
            config.setLocales(localeList)
            super.attachBaseContext(base.createConfigurationContext(config))
        } else {
            super.attachBaseContext(base)
        }
    }

}

Prepare as many strings.xml as you need

An example of preparing all 11 languages with English as the default

main/res/values/strings.xml
main/res/values-de/strings.xml
main/res/values-es/strings.xml
main/res/values-fr/strings.xml
main/res/values-it/strings.xml
main/res/values-ja/strings.xml
main/res/values-nl/strings.xml
main/res/values-pl/strings.xml
main/res/values-pt/strings.xml
main/res/values-ru/strings.xml
main/res/values-zh/strings.xml

The arguments given to Locale() are as follows.
"en"
"de"
"es"
"fr"
"it"
"ja"
"nl"
"pl"
"pt"
"ru"
"zh"

Support for SDK Version 23 and below

    //Language setting
    override fun attachBaseContext(base: Context) {
        val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
        val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
        if (loc != null) {
            val res = base.resources
            val config = Configuration(res.configuration)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {    //minSdkVersion 24
                val localeList = LocaleList(loc)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
            } else {    //minSdkVersion 17
                config.setLocale(loc)
            }
            super.attachBaseContext(base.createConfigurationContext(config))
        } else {
            super.attachBaseContext(base)
        }
    }
CONTENTS
Web Browser