Supporting Multiple Languages in Android

Introduction

 
This article explains how your application can support various languages in Android using Android Studio.
 
This application supports the two languages, English and Spanish. So when you start the application its layout contains the English language because it is already set in the locale of the emulator. To start your application in the Spanish language you need to set the Spanish inside the locale. After doing that when you start your application its layout uses the Spanish language. 
 
When we create the project there is string.xml file inside the res->values. It is created by default by the system when you create the project.This file contains:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <string name="app_name">SupportingDifferentLanguages</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="enter_name">Enter Name</string>  
  7.     <string name="enter_age">Enter Age</string>  
  8.     <string name="enter">Submit</string>  
  9. </resources> 
It is set by default in your application XML file when you create the project. So to make your application support a different language you need to do this. 
 
In this first, we create a folder named value-es inside the res folder. We use values-es because in the locale es is the code word for Spanish. In the value-es folder, we will create an XML file named string.xml. Inside the string.xml file we will write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <string name="enter_name">ingrese el nombre</string>  
  5.     <string name="enter_age">entrar en la edad</string>  
  6.     <string name="enter">presentar</string>  
  7.    
  8. </resources> 
You will set the language text in your layout by writing this in your MainActivity.
  1. TextView textview=new TextView(this);  
  2.    
  3.         TextView textview2=new TextView(this);  
  4.         Button button=new Button(this);  
  5.         textview.setText(R.string.enter_name);  
  6.         textview2.setText(R.string.enter_age);  
  7.         button.setText(R.string.enter); 
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:id="@+id/textview"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/enter_name"  
  16.         android:textStyle="bold"  
  17.            />  
  18. <EditText  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_width="300dp"  
  21.         android:layout_marginTop="20dp"></EditText>  
  22.    
  23.     <TextView  
  24.             android:id="@+id/textview2"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="@string/enter_age"  
  28.             android:textStyle="bold"  
  29.             android:layout_marginTop="80dp"  
  30. />  
  31.    
  32.     <EditText  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_width="300dp"  
  35.             android:layout_marginTop="100dp"></EditText>  
  36.    
  37. <Button  
  38.     android:layout_height="wrap_content"  
  39.         android:layout_width="100dp"  
  40.         android:layout_marginTop="180dp"  
  41.         android:text="@string/enter"  
  42.         android:id="@+id/button"/>  
  43.    
  44. </RelativeLayout> 
Step 2
 
Create a string.xml file inside value-es as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <string name="enter_name">ingrese el nombre</string>  
  5.     <string name="enter_age">entrar en la edad</string>  
  6.     <string name="enter">presentar</string>  
  7.    
  8. </resources> 
Step 3
 
Create a Java class file and write this:
  1. package com.supportingdifferentlanguages;  
  2.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.    
  9. public class MainActivity extends Activity {  
  10.    
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.    
  16.         TextView textview=new TextView(this);  
  17.    
  18.         TextView textview2=new TextView(this);  
  19.         Button button=new Button(this);  
  20.         textview.setText(R.string.enter_name);  
  21.         textview2.setText(R.string.enter_age);  
  22.         button.setText(R.string.enter);  
  23.    
  24.     }  
  25.    
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // Inflate the menu; this adds items to the action bar if it is present.  
  29.         getMenuInflater().inflate(R.menu.main, menu);  
  30.         return true;  
  31.     }  
  32.    
Step 4
 
Image 1
 
Clipboard02.jpg
 
Image 2
 
Clipboard07.jpg


Similar Articles