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. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world"
  14. android:id="@+id/text"/>
  15. <EditText
  16. android:id="@+id/edit"
  17. android:layout_height="wrap_content"
  18. android:layout_width="200dp"
  19. android:layout_marginTop="30dp">
  20. </EditText>
  21. <Button
  22. android:id="@+id/button"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginTop="80dp"
  26. android:text="@string/send_message"></Button>
  27. </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. <TextView
  7. android:id="@+id/textview"
  8. android:layout_height="wrap_content"
  9. android:textSize="40dp"
  10. android:textStyle="bold"
  11. android:layout_width="wrap_content">
  12. </TextView>
  13. </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. import java.util.Locale;
  19. public class MainActivity extends Activity
  20. {
  21. private Context context;
  22. private Character language_code;
  23. TextView textview;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState)
  26. {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. String languageToLoad = "es"; // your language
  30. Locale locale = new Locale(languageToLoad);
  31. Locale.setDefault(locale);
  32. Configuration config = new Configuration();
  33. config.locale = locale;
  34. getBaseContext().getResources().updateConfiguration(config,
  35. getBaseContext().getResources().getDisplayMetrics());
  36. this.setContentView(R.layout.activity_main);
  37. textview = (TextView) findViewById(R.id.text);
  38. EditText editText = (EditText) findViewById(R.id.edit);
  39. Button button = (Button) findViewById(R.id.button);
  40. button.setOnClickListener(new View.OnClickListener()
  41. {
  42. @Override
  43. public void onClick(View view)
  44. {
  45. Intent i = new Intent(MainActivity.this, Second.class);
  46. Bundle b = new Bundle();
  47. b.putString("1", "Value");
  48. i.putExtras(b);
  49. startActivity(i);
  50. }
  51. });
  52. }
  53. @Override
  54. public boolean onCreateOptionsMenu(Menu menu)
  55. {
  56. // Inflate the menu; this adds items to the action bar if it is present.
  57. getMenuInflater().inflate(R.menu.main, menu);
  58. return true;
  59. }
  60. @Override
  61. public boolean onOptionsItemSelected(MenuItem item)
  62. {
  63. switch (item.getItemId())
  64. {
  65. case R.id.actionmenu:
  66. /*ActionBar actionBar=getActionBar();
  67. actionBar.hide();
  68. actionBar.show();*/
  69. // Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
  70. // locked=false;
  71. // invalidateOptionsMenu();
  72. // ActivityCompat.invalidateOptionsMenu(MainActivity.this);
  73. Intent i = new Intent(MainActivity.this, Second.class);
  74. Bundle b = new Bundle();
  75. b.putString("1", "Value");
  76. i.putExtras(b);
  77. startActivity(i);
  78. break;
  79. /* case R.id.menuitem2:
  80. Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
  81. .show();
  82. break;*/
  83. default
  84. break;
  85. }
  86. return true;
  87. }
  88. public void actionMethod(View v)
  89. {
  90. TextView textView = new TextView(this);
  91. textView.setText(R.string.hello_world);
  92. }
  93. }
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. * Created by vivek on 8/19/13.
  8. */
  9. public class Second extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.second);
  14. TextView textView=(TextView)findViewById(R.id.textview);
  15. // Bundle b=getIntent().getExtras();
  16. Intent i=getIntent();
  17. Bundle b=i.getExtras();
  18. String x= b.getString("1");
  19. textView.setText(x);
  20. }
  21. }
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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.myapplication.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name=".Second"/>
  24. </application>
  25. </manifest>
Step 10
Image
Clipboard02.jpg