title: Android: How to change app language programmatically?
body: <p>I want to change my app language programatically. I have some code which is working on older phones ( Android 6) but it is not working on Android 8 and Android 9. Any working solution for app language change? After calling setLocal I call recreate() inside Activity. Still no changes in strings.</p>
  
  <p>In my <code>MainActivity</code> which is extending <code>BaseActivity</code> in <code>onCreate()</code> If I call <code>Locale.getDefault().language</code> it is returning correct language code but strings are still in English which is default <code>string.xml</code>.</p>
  
  <pre><code>fun setLocale(context: Context, language: String?): Context {
      app.setLanguage(language)
  
      return if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
          updateResources(context, language)
      } else updateResourcesLegacy(
          context,
          language
      )
  
  }
  
  @TargetApi(Build.VERSION_CODES.N)
  private fun updateResources(context: Context, language: String?): Context {
      val locale =
      if (language == null){
          Locale(Locale.getDefault().language)
      } else {
          Locale(language)
      }
  
      Locale.setDefault(locale)
      val configuration = context.resources.configuration
      configuration.setLocale(locale)
  
      return context.createConfigurationContext(configuration)
  }
  
  @Suppress("DEPRECATION")
  private fun updateResourcesLegacy(context: Context, language: String?): Context {
      val locale =
          if (language == null){
              Locale(Locale.getDefault().language)
          } else {
              Locale(language)
          }
  
      Locale.setDefault(locale)
      val resources = context.resources
  
      val configuration = resources.configuration
      configuration.locale = locale
  
      resources.updateConfiguration(configuration, resources.displayMetrics)
  
      return context
  }
  </code></pre>
  
  <p>UPDATE: 
  Ive used combination of both solutions below, but still without success. I made <code>BaseActivity</code> class which is extended by all of my activities. And there I call <code>changeLocale</code> function which is similar to LocaleHelper. <code>app.getSavedLanguage()</code> returns saved language code in my <code>sharedPrefs</code>. This code is overwritten based on which language user choose in app. App is Application class which is working with shared preferences.</p>
  
  <pre><code>override fun onCreate(si: Bundle?) {
          super.onCreate(si)
          app = application as App
          changeLocale(this, app.getSavedLanguage())
      }
  
      open fun changeLocale(context: Context, lang: String) {
          val newLocale = Locale(lang)
          Locale.setDefault(newLocale)
  
          val res: Resources = context.resources
          val conf: Configuration = res.configuration
  
          if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N){
              conf.apply {
                  setLocale(newLocale)
                  setLayoutDirection(newLocale)
              }
  
              context.createConfigurationContext(conf)
          } else {
              conf.apply {
                  locale = newLocale
                  setLayoutDirection(newLocale)
              }
  
              res.updateConfiguration(conf, res.displayMetrics)
          }
      }
  </code></pre>