title: Changing language programmatically in Android
body: <p>Well, to begin with, I have set different resource files for each language as Google says. I have a separate Class called <code>LocaleHelper.class</code> to help me display the String values for each Language programmatically.</p>
  
  <p>The translation works perfectly when displaying a String resource. But, when I try to display a String Array, then comes the issue. The items in String Array always displays the English words for every language</p>
  
  <p>To illustrate, I have a String Array for the Spinner as below</p>
  
  <pre><code>&lt;string-array name="SpinnerList_Number"&gt;
      &lt;item&gt;@string/Number_One&lt;/item&gt;
      &lt;item&gt;@string/Number_Two&lt;/item&gt;
      &lt;item&gt;@string/Number_Three&lt;/item&gt;
  &lt;/string-array&gt;
  </code></pre>
  
  <p>where,</p>
  
  <p>for English:</p>
  
  <pre><code>&lt;string name="Number_One"&gt;One&lt;/string&gt;
  </code></pre>
  
  <p>for German:</p>
  
  <pre><code>&lt;string name="Number_One"&gt;Eins&lt;/string&gt;
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>In my Activity/Fragment, I use the following code to display the text in required locale</p>
  
  <pre><code>Context context = LocaleHelper.setLocale(this, AppLanguage);
  context.getResources().getString(R.string.Number_One)
  </code></pre>
  
  <p>To display a String Array (say in a Spinner), I use <code>R.array.SpinnerList_Numbers</code>. How can I use <code>context.getResources()</code> to display the String Array in Java code?</p>
  
  <p>The following is the Code for <code>LocaleHelper.class</code> file</p>
  
  <pre><code>public class LocaleHelper {
  
      private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
  
      public static Context onAttach(Context context) {
          String lang = getPersistedData(context, Locale.getDefault().getLanguage());
          return setLocale(context, lang);
      }
  
      public static Context onAttach(Context context, String defaultLanguage) {
          String lang = getPersistedData(context, defaultLanguage);
          return setLocale(context, lang);
      }
  
      public static String getLanguage(Context context) {
          return getPersistedData(context, Locale.getDefault().getLanguage());
      }
  
      public static Context setLocale(Context context, String language) {
          persist(context, language);
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
              return updateResources(context, language);
          }
  
          return updateResourcesLegacy(context, language);
      }
  
      private static String getPersistedData(Context context, String defaultLanguage) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
      }
  
      private static void persist(Context context, String language) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
          SharedPreferences.Editor editor = preferences.edit();
  
          editor.putString(SELECTED_LANGUAGE, language);
          editor.apply();
      }
  
      @TargetApi(Build.VERSION_CODES.N)
      private static Context updateResources(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Resources resources = context.getResources();
  
          Configuration configuration = resources.getConfiguration();
          configuration.setLocale(locale);
          configuration.setLayoutDirection(locale);
  
          return context.createConfigurationContext(configuration);
      }
  
      @SuppressWarnings("deprecation")
      private static Context updateResourcesLegacy(Context context, String language) {
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Resources resources = context.getResources();
  
          Configuration configuration = resources.getConfiguration();
          configuration.locale = locale;
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
              configuration.setLayoutDirection(locale);
          }
  
          resources.updateConfiguration(configuration, resources.getDisplayMetrics());
  
          return context;
      }
  }
  </code></pre>
  
  <p><strong>Update: Solution</strong></p>
  
  <p>As mentioned by @MiteshVanaliya, here is the code for the correct solution while using the <code>LocaleHelper.class</code></p>
  
  <pre><code>ArrayAdapter&lt;CharSequence&gt; adapterSpinner = new ArrayAdapter&lt;CharSequence&gt;(
          context,
          android.R.layout.simple_spinner_item,
          context.resources().getStringArray(R.array.SpinnerList_Number));
  adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  mSpinner.setAdapter(adapterSpinner);
  </code></pre>