App Localization in Android
What is Localization?
Localization is the process of adapting your app's user interface and content to
specific regions or languages. It allows apps to cater to different language
preferences of users around the world. Android supports localization by
providing alternative resources based on the device's locale.
Why is Localization Important?
Global Reach: Extends the app’s reach to different languages and
cultures.
User Preference: Allows users to interact with the app in their preferred
language.
Market Penetration: Essential for apps targeting international audiences.
Localization in Android
Android manages localization by using resource qualifiers. You can create
different resource files for each language and Android will automatically load
the appropriate resources based on the user's language settings.
Objective: Build a Simple Android App
Supporting English and Spanish
Instructions:
In this task, we will build a simple Android app with the
following requirements:
- The app will support English and Spanish languages.
- A button will allow the user to switch languages dynamically
using the `setLocale()` method.
- We will also test the app by changing the device's language to
ensure correct string display.
Step-by-Step Instructions :
Step 1: Create a New Android Project
1. Open Android Studio
2. Select New Project and choose Empty Activity.
3. Set the app name (e.g., "applocalization").
4. Select Kotlin as the programming language.
5. Set the minimum API level to 21 (Android 5.0 Lollipop).
6. Click Finish to create the project.
Step 2: Add `strings.xml` for English and Spanish
We need to create separate string resources for English (default) and
Spanish.
Step 2.1: Default `strings.xml` for English
Go to `res/values/strings.xml` and edit the file to add a welcome message
and a button label:
<resources>
<string name="app_name">MultiLanguageApp</string>
<string name="welcome_message">Welcome to the Multi-Language
App!</string>
<string name="change_language">Change Language</string>
</resources>
Step 2.2: Create `strings.xml` for Spanish
- Create a new resource directory for Spanish:
- Right-click on the `res` folder and choose New > Android Resource
Directory.
- In the Directory name, append `-es` to `values` (making it `values-es`).
- Click OK.
- Inside `res/values-es/`, create a new `strings.xml` file:
- Right-click on `values-es` and select **New > Values Resource File**.
- Name the file `strings.xml`.
- Add Spanish translations in `res/values-es/strings.xml`:
<resources>
<string name="app_name">Aplicación MultiLenguaje</string>
<string name="welcome_message">¡Bienvenido a la Aplicación
MultiLenguaje!</string>
<string name="change_language">Cambiar Idioma</string>
</resources>
Step 3: Design the UI in `activity_main.xml
The layout will include a `TextView` for the welcome message and a
`Button` to trigger language change.
1. Open `res/layout/activity_main.xml` and replace the existing content
with the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/welcomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_message"
android:textSize="18sp"
android:padding="20dp"/>
<Button
android:id="@+id/changeLanguageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_language"
android:onClick="changeLanguage"/>
</LinearLayout>
This layout consists of:
- A `TextView` to display the welcome message.
- A `Button` to change the language dynamically.
Step 4: Implement Language Switching in Kotlin
We will implement the functionality to change the language when the
button is clicked.
1. Open the `MainActivity.kt` file in
`java/com.example.multilanguageapp`.
2. Add the following code to handle dynamic language switching:
package com.example.multilanguageapp
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
private var isEnglish = true // Track current language
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Set initial welcome message
val welcomeTextView: TextView =
findViewById(R.id.welcomeTextView)
welcomeTextView.text = getString(R.string.welcome_message)
// Method to change the language
fun changeLanguage(view: View) {
if (isEnglish) {
setLocale("es") // Switch to Spanish
} else {
setLocale("en") // Switch to English
isEnglish = !isEnglish // Toggle language
}
// Method to set the locale
private fun setLocale(languageCode: String) {
val locale = Locale(languageCode)
Locale.setDefault(locale)
val config = Configuration()
config.setLocale(locale)
// Update the configuration
resources.updateConfiguration(config, resources.displayMetrics)
// Refresh the activity
recreate()
```
- The app starts with the default language (English).
- A boolean flag `isEnglish` is used to track the current language.
- The `changeLanguage()` method switches between English (`en`) and
Spanish (`es`) when the button is clicked.
- The `setLocale()` method updates the app’s configuration and refreshes
the activity to apply the language change.
---
Step 5: Run and Test the App
Step 6: Test Language Switching Based on Device Settings
1. Open the Settings app on the device or emulator.
2. Go to Languages & Input and change the device language to Spanish.
3. Open the app, and it should display all text in Spanish automatically
without the need to click the button.