title: Android - WebView language changes abruptly on Android 7.0 and above
body: <p>I have a multilingual app with primary language English and secondary language Arabic.</p>
  
  <p>As described in the <a href="https://developer.android.com/about/versions/android-4.2.html#RTL" rel="noreferrer">documentation</a>,</p>
  
  <ul>
  <li>I have added <code>android:supportsRtl="true"</code> in the manifest.</li>
  <li>I have changed all xml properties with <code>left</code> and <code>right</code> attributes to <code>start</code> and <code>end</code> respectively.</li>
  <li>I have added Arabic language strings in <code>strings-ar</code> (and similarly for other resources).</li>
  </ul>
  
  <p>The above setup works properly. After changing the <code>Locale</code> to <code>ar-AE</code>, Arabic text &amp; resources are correctly displayed in my Activities.</p>
  
  <blockquote>
    <p>However, every time I navigate to an <code>Activity</code> with a <code>WebView</code>
    and/or a <code>WebViewClient</code>, the locale, text and layout direction
    abruptly revert to the device default.</p>
  </blockquote>
  
  <p><strong>Further hints:</strong></p>
  
  <ul>
  <li>This is occurring <strong><em>only</em></strong> on a Nexus 6P with <strong>Android 7.0</strong>. Everything works properly on Android 6.0.1 and below.</li>
  <li>The abrupt shift in locale happens <strong><em>only</em></strong> when I navigate to an <code>Activity</code> that has a <code>WebView</code> and/or a <code>WebViewClient</code> (and I have several). It does not occur on any of the other Activities.</li>
  </ul>
  
  <p>Android 7.0 has multi-locale support, allowing the user to set more than one default locale. So if I set the primary locale to <code>Locale.UK</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/gPxVk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gPxVk.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Then on navigating to the <code>WebView</code>, the locale changes from <code>ar-AE</code>
    to <code>en-GB</code>.</p>
  </blockquote>
  
  <p><strong>Android 7.0 API changes:</strong></p>
  
  <p>As indicated in the <a href="https://developer.android.com/sdk/api_diff/24/changes.html" rel="noreferrer">list of API changes</a>, new methods pertaining to locale have been added to the following classes in API 24:</p>
  
  <p><strong><code>Locale</code>:</strong></p>
  
  <ul>
  <li><a href="https://developer.android.com/reference/java/util/Locale.html#getDefault%28java.util.Locale.Category%29" rel="noreferrer"><code>Locale.getDefault(...)</code></a></li>
  <li><a href="https://developer.android.com/reference/java/util/Locale.html#setDefault%28java.util.Locale.Category,%20java.util.Locale%29" rel="noreferrer"><code>Locale.setDefault(...)</code></a></li>
  </ul>
  
  <p><strong><code>Configuration</code>:</strong></p>
  
  <ul>
  <li><a href="https://developer.android.com/reference/android/content/res/Configuration.html#getLocales%28%29" rel="noreferrer"><code>getLocales()</code></a></li>
  <li><a href="https://developer.android.com/reference/android/content/res/Configuration.html#setLocales%28android.os.LocaleList%29" rel="noreferrer"><code>setLocales(...)</code></a></li>
  </ul>
  
  <blockquote>
    <p>However, I am building my app with API 23, and am not using any of
    these new methods.</p>
  </blockquote>
  
  <p><strong>Furthermore ...</strong></p>
  
  <ul>
  <li><p>The problem occurs on the Nexus 6P emulator as well.</p></li>
  <li><p>To get the default locale, I am using <a href="https://developer.android.com/reference/java/util/Locale.html#getDefault%28%29" rel="noreferrer"><code>Locale.getDefault()</code></a>.</p></li>
  <li><p>To set the default locale, I am using the following code:</p>
  
  <pre><code>public static void setLocale(Locale locale){
      Locale.setDefault(locale);
      Configuration config = new Configuration();
      config.setLocale(locale);
      Context context = MyApplication.getInstance();
      context.getResources().updateConfiguration(config,
              context.getResources().getDisplayMetrics());
  }
  </code></pre></li>
  </ul>
  
  <p>Has anyone encountered this problem before? What is the reason for it, and how do I resolve this?</p>
  
  <p><strong>References:</strong></p>
  
  <p><strong>1.</strong> <a href="http://android-developers.blogspot.in/2013/03/native-rtl-support-in-android-42.html" rel="noreferrer"><strong>Native RTL support in Android 4.2</strong></a>.</p>
  
  <p><strong>2.</strong> <a href="https://developer.android.com/guide/topics/resources/multilingual-support.html" rel="noreferrer"><strong>Multilingual Support - Language and Locale</strong></a>.</p>
  
  <p><strong>3.</strong> <a href="https://developer.android.com/reference/java/util/Locale.html" rel="noreferrer"><strong>Be wary of the default locale</strong></a>.</p>