Android Multi Language Support

By | March 18, 2014
Android Runs on many devices in many regions .  if you want your application should reach more and more of number devices in many countries ,then you have to localize the text according to the country/region  specific

In this tutorial , we will see a simple login example which is localized to English(Default) , Spanish , French , Portuguese.


Lets See An Example

Project Detail

Project Name Language_Support
Package com.pavan.languagesupport
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

String Constants

To add support for more languages , create additional values directories inside res/ that include hypen and country locale code at the end and then inside this directory we will create strings.xml were will define the string constant

for ex :- /res/values/strings.xml

Locale Code Language / Country Location of strings.xml
Default English / United Kingdom res/values/
es Spanish / Spain res/values-es/
fr French / France res/values-fr/
pt Portuguese / Brazil res/values-pt/

It is good practice to make use of  Android resource framework to separate the localized aspects of your application according to the country specific

follow the below steps of how to create resource for country specific

  1. Right click over the project and then goto “new” -> “others”->”Android XML Values File” .
  2. Give the file name xyz.xml click next . 
  3. Select the Language Qualifier .
  4. Select the Country Code from the drop down .

English (Default)

file : /res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

    <string name=”app_name”>Language_Support</string>
    <string name=”action_settings”>Settings</string>
    <string name=”label_username”>UserName</string>
    <string name=”password”>Password</string>
    <string name=”btn_sign”>Sign In</string>

</resources>

Spanish

file : /res/values-es/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

    <string name=”app_name”>idioma_apoyo</string>
    <string name=”action_settings”>Configuración</string>
    <string name=”label_username”>Nombre de usuario</string>
    <string name=”password”>contraseña</string>
    <string name=”btn_sign”>ingresar</string>

</resources>

French

file : /res/values-fr/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

    <string name=”app_name”>Support de la langue</string>
    <string name=”action_settings”>paramètres</string>
    <string name=”label_username”>nom d’utilisateur</string>
    <string name=”password”>mot de passe</string>
    <string name=”btn_sign”>Identifiez-vous</string>

</resources>

Portuguese

file : /res/values-pt/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

    <string name=”app_name”>Suporte ao Idioma</string>
    <string name=”action_settings”>definições</string>
    <string name=”label_username”>nome de usuário</string>
    <string name=”password”>senha</string>
    <string name=”btn_sign”>Entrar</string>

</resources>

XML Layout

file : activity_main.xml

<RelativeLayout 
    xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:paddingBottom=”@dimen/activity_vertical_margin”
    android:paddingLeft=”@dimen/activity_horizontal_margin”
    android:paddingRight=”@dimen/activity_horizontal_margin”
    android:paddingTop=”@dimen/activity_vertical_margin”
    tools:context=”.MainActivity” >

    <TextView
        android:id=”@+id/textView1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”@string/label_username”
        android:textAppearance=”?android:attr/textAppearanceMedium” />

    <EditText
        android:id=”@+id/editText1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/textView1″
        android:layout_below=”@+id/textView1″
        android:layout_marginLeft=”23dp”
        android:ems=”10″ >

        <requestFocus />
    </EditText>

    <TextView
        android:id=”@+id/textView2″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/textView1″
        android:layout_below=”@+id/editText1″
        android:layout_marginTop=”30dp”
        android:text=”@string/password”
        android:textAppearance=”?android:attr/textAppearanceMedium” />

    <EditText
        android:id=”@+id/editText2″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/editText1″
        android:layout_below=”@+id/textView2″
        android:ems=”10″ />

    <Button
        android:id=”@+id/button1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentRight=”true”
        android:layout_below=”@+id/editText2″
        android:layout_marginRight=”36dp”
        android:layout_marginTop=”27dp”
        android:text=”@string/btn_sign” />

</RelativeLayout>

Main Activity

file : MainActivity.java

package com.pavan.languagesupport;

import com.pavan.langsupport.R;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}