How to Change Locale of an Application Dynamically in Android

Introduction

 
This article explains how to dynamically change the locale of Android. Android Studio is used to create the sample.
 
We understand that applications support various languages in Android. So to perform language support operations for applications the user changes the language of the locale by clicking on the list of languages. Now the question is, how to change the language of your application dynamically without going to the custom locale of Android.
 
We need this when we want the application to run only in the Spanish language, not in the phone's default language. So we do this by writing the following code in the onCreate () method:
  1. String languageToLoad = "es"// your language  
  2. Locale locale = new Locale(languageToLoad);  
  3. Locale.setDefault(locale);  
  4. Configuration config = new Configuration();  
  5. config.locale = locale;  
  6. getBaseContext().getResources().updateConfiguration(config,  
  7.  getBaseContext().getResources().getDisplayMetrics());  
  8. this.setContentView(R.layout.activity_main); 
Local: a class to set the language of your application.
 
Configuration: a class describing all device configuration information that can impact resources the application retrieves.
 
First, we create a variable of String type named languageToLoad and "es" as a value to the variable. Now create a Locale object and call the static method setDefault(). the object in setDefault() as a parameter to set the language. Create the configuration object that returns the configuration of the device.
 
Step 1
  
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world"  
  15.             android:id="@+id/text"/>  
  16.    
  17.     <EditText  
  18.             android:id="@+id/edit"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_width="200dp"  
  21.             
  22.             android:layout_marginTop="30dp">  
  23.             </EditText>  
  24.     <Button  
  25.             android:id="@+id/button"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_marginTop="80dp"  
  29.             android:text="@string/send_message"></Button>  
  30. </RelativeLayout> 
Step 2
 
Create another XML file with the following:
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="vertical"  
  4.               android:layout_width="match_parent"  
  5.               android:layout_height="match_parent">  
  6.    
  7.     <TextView  
  8.             android:id="@+id/textview"  
  9.             android:layout_height="wrap_content"  
  10.             android:textSize="40dp"  
  11.             android:textStyle="bold"  
  12.             android:layout_width="wrap_content">  
  13.      </TextView>  
  14. </LinearLayout> 
Step 3
 
Create a Java file with the following:
  1. package com.myapplication;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.content.res.Configuration;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.app.Activity;  
  8. import android.support.v4.app.ActivityCompat;  
  9. import android.util.DisplayMetrics;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.Switch;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.   
  19. import java.util.Locale;  
  20.   
  21. public class MainActivity extends Activity   
  22. {  
  23.  private Context context;  
  24.  private Character language_code;  
  25.   
  26.  TextView textview;  
  27.  @Override  
  28.  protected void onCreate(Bundle savedInstanceState)   
  29.  {  
  30.   super.onCreate(savedInstanceState);  
  31.   setContentView(R.layout.activity_main);  
  32.   String languageToLoad = "es"// your language  
  33.   Locale locale = new Locale(languageToLoad);  
  34.   Locale.setDefault(locale);  
  35.   Configuration config = new Configuration();  
  36.   config.locale = locale;  
  37.   getBaseContext().getResources().updateConfiguration(config,  
  38.    getBaseContext().getResources().getDisplayMetrics());  
  39.   this.setContentView(R.layout.activity_main);  
  40.   
  41.   textview = (TextView) findViewById(R.id.text);  
  42.   EditText editText = (EditText) findViewById(R.id.edit);  
  43.   Button button = (Button) findViewById(R.id.button);  
  44.   button.setOnClickListener(new View.OnClickListener()   
  45.   {  
  46.    @Override  
  47.    public void onClick(View view)   
  48.    {  
  49.     Intent i = new Intent(MainActivity.this, Second.class);  
  50.     Bundle b = new Bundle();  
  51.     b.putString("1""Value");  
  52.     i.putExtras(b);  
  53.     startActivity(i);  
  54.    }  
  55.   });  
  56.  }  
  57.  @Override  
  58.  public boolean onCreateOptionsMenu(Menu menu)   
  59.  {  
  60.   // Inflate the menu; this adds items to the action bar if it is present.  
  61.   getMenuInflater().inflate(R.menu.main, menu);  
  62.   return true;  
  63.  }  
  64.  @Override  
  65.  public boolean onOptionsItemSelected(MenuItem item)   
  66.  {  
  67.   switch (item.getItemId())   
  68.   {  
  69.    case R.id.actionmenu:  
  70.     /*ActionBar actionBar=getActionBar(); 
  71.       actionBar.hide(); 
  72.       actionBar.show();*/  
  73.     //   Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();  
  74.     //  locked=false;  
  75.     // invalidateOptionsMenu();  
  76.     //   ActivityCompat.invalidateOptionsMenu(MainActivity.this);  
  77.     Intent i = new Intent(MainActivity.this, Second.class);  
  78.     Bundle b = new Bundle();  
  79.     b.putString("1""Value");  
  80.     i.putExtras(b);  
  81.     startActivity(i);  
  82.     break;  
  83.     /*   case R.id.menuitem2: 
  84.          Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT) 
  85.          .show(); 
  86.           break;*/  
  87.    default  
  88.    break;  
  89.   }  
  90.   return true;  
  91.  }  
  92.  public void actionMethod(View v)   
  93.  {  
  94.   TextView textView = new TextView(this);  
  95.   textView.setText(R.string.hello_world);  
  96.  }  
Step 4
 
Create another Java class file with the following:
  1. package com.myapplication;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.    
  7. /** 
  8.  * Created by vivek on 8/19/13. 
  9.  */  
  10. public class Second extends Activity {  
  11.    
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.second);  
  16.         TextView textView=(TextView)findViewById(R.id.textview);  
  17.    
  18.        // Bundle b=getIntent().getExtras();  
  19.        Intent i=getIntent();  
  20.         Bundle b=i.getExtras();  
  21.         String x=  b.getString("1");  
  22.         textView.setText(x);  
  23.     }  
Step 5
 
The following is the String.xml file in the value folder:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">Application</string>  
  4.     <string name="action_settings">Settings</string>  
  5.     <string name="hello_world">Hello world!</string>  
  6.     <string name="enter_the_meassage">Enter The Message</string>  
  7.     <string name="send_message">Send</string>  
  8. </resources> 
Step 6
 
The following is the String.xml file in the value-de folder:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.      <string name="hello_world">geben Sie den Namen</string>  
  4.      <string name="send_message"></string>  
  5. </resources> 
Step 7
 
The following is the String.xml file in the value-es folder:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">Application</string>  
  4.     <string name="action_settings">Settings</string>  
  5.     <string name="enter_the_meassage">Enter The Message</string>  
  6.     <string name="send_message">enviar mensajes</string>  
  7.     <string name="hello_world">introduzca el nombre</string>  
  8. </resources> 
Step 8
 
The following is the String.xml file in the value-hr folder:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">Application</string>  
  4.     <string name="action_settings">Settings</string>  
  5.     <string name="hello_world">Hola wrldo!</string>  
  6.     <string name="enter_the_meassage">Enter The Message</string>  
  7.     <string name="send_message">pošalji poruku</string>  
  8. </resources> 
Step 9
 
Perform the following changes in the Androidmanifest.Xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.myapplication"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.myapplication.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     <activity  
  26.             android:name=".Second"/>  
  27.     </application>  
  28.    
  29. </manifest> 
Step 10
 
Image
 
Clipboard02.jpg


Similar Articles