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 aplication its layout contains the English language because it is already set in the locale of the emulator. To start your application in 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:

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="app_name">SupportingDifferentLanguages</string>

    <string name="action_settings">Settings</string>

    <string name="enter_name">Enter Name</string>

    <string name="enter_age">Enter Age</string>

    <string name="enter">Submit</string>

</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:

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="enter_name">ingrese el nombre</string>

    <string name="enter_age">entrar en la edad</string>

    <string name="enter">presentar</string>

 

</resources>

You will set the language text in your layout by writting this in your MainActivity.
 

TextView textview=new TextView(this);

 

        TextView textview2=new TextView(this);

        Button button=new Button(this);

        textview.setText(R.string.enter_name);

        textview2.setText(R.string.enter_age);

        button.setText(R.string.enter);


Step 1

Create an XML file and write this:

<
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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">

 

    <TextView

       android:id="@+id/textview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/enter_name"

        android:textStyle="bold"

           />

<EditText

        android:layout_height="wrap_content"

        android:layout_width="300dp"

        android:layout_marginTop="20dp"></EditText>

 

    <TextView

            android:id="@+id/textview2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/enter_age"

            android:textStyle="bold"

            android:layout_marginTop="80dp"

/>

 

    <EditText

            android:layout_height="wrap_content"

            android:layout_width="300dp"

            android:layout_marginTop="100dp"></EditText>

 

<Button

    android:layout_height="wrap_content"

        android:layout_width="100dp"

        android:layout_marginTop="180dp"

        android:text="@string/enter"

        android:id="@+id/button"/>

 

</RelativeLayout>


Step 2

Create a string.xml file inside value-es as in the following:
 

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="enter_name">ingrese el nombre</string>

    <string name="enter_age">entrar en la edad</string>

    <string name="enter">presentar</string>

 

</resources>

Step 3

Create a Java class file and write this:

 

package com.supportingdifferentlanguages;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.Button;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        TextView textview=new TextView(this);

 

        TextView textview2=new TextView(this);

        Button button=new Button(this);

        textview.setText(R.string.enter_name);

        textview2.setText(R.string.enter_age);

        button.setText(R.string.enter);

 

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

}

Step 4

Image 1

Clipboard02.jpg


Image 2

Clipboard07.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all