title: Android Change language in all activities
body: <p>I know such questions have been asked before but they could not help me.
  I'm creating a simple app in which i want the app to convert in Urdu from English as i can change the first activity language but don't know how to change language in second activity <strong>(whole Application)</strong>. in second activity I've single textView which i want to change also when i change language from first activity by button to choose which language i want to have. I've created resources in string.</p>
  <p>Here is my code</p>
  <p><strong>XML</strong></p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/button4&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;@string/find_Mazdoor&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.498&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintVertical_bias=&quot;0.2&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/button5&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;@string/mazdoor_register&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.498&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/button4&quot;
          app:layout_constraintVertical_bias=&quot;0.04&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/language&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;@string/change_language&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.498&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/button5&quot;
          app:layout_constraintVertical_bias=&quot;0.204&quot; /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><strong>MainActivity</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      Button language_btn, find_mazdoor;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          loadLocale();
          setContentView(R.layout.activity_main);
  
          ActionBar actionBar = getSupportActionBar();
          assert actionBar != null;
          actionBar.setTitle(getResources().getString(R.string.app_name));
  
          find_mazdoor = findViewById(R.id.button4);
          find_mazdoor.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                  startActivity(intent);
              }
          });
  
          language_btn = findViewById(R.id.language);
          language_btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  changeLanguage();
              }
          });
      }
  
      private void changeLanguage() {
  
          final String[] language = {&quot;English&quot;, &quot;اردو&quot;};
  
          AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
          mBuilder.setTitle(&quot;Change Language...&quot;);
          mBuilder.setSingleChoiceItems(language, -1, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  if (which == 0) {
  
                      //English
                      setLocale(&quot;en&quot;);
                      recreate();
                  } else if (which == 1) {
  
                      //Urdu
                      setLocale(&quot;ur&quot;);
                      recreate();
                  }
  
                  dialog.dismiss();
              }
          });
  
          AlertDialog mDialog = mBuilder.create();
          mDialog.show();
      }
  
      private void setLocale(String lang) {
  
          Locale locale = new Locale(lang);
          Locale.setDefault(locale);
  
          Configuration config = new Configuration();
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              config.setLocale(locale);
          }
          getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
  
          SharedPreferences.Editor editor = getSharedPreferences(&quot;Settings&quot;, MODE_PRIVATE).edit();
          editor.putString(&quot;My_Lang&quot;, lang);
          editor.apply();
      }
  
      public void loadLocale() {
  
          SharedPreferences prefs = getSharedPreferences(&quot;Settings&quot;, Activity.MODE_PRIVATE);
          String language = prefs.getString(&quot;My_Lang&quot;, &quot;&quot;);
          setLocale(language);
      }
  }
  </code></pre>