0% found this document useful (0 votes)
13 views14 pages

Mobile Device Programming Lab 04

This document outlines the process of implementing localization in Android applications, focusing on adapting user interfaces and content for multiple languages and regions. It includes learning objectives, terminology, and practical exercises to create applications that support localization using resource files. The document emphasizes the importance of testing and best practices to ensure a user-friendly experience for a global audience.

Uploaded by

dungnq222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

Mobile Device Programming Lab 04

This document outlines the process of implementing localization in Android applications, focusing on adapting user interfaces and content for multiple languages and regions. It includes learning objectives, terminology, and practical exercises to create applications that support localization using resource files. The document emphasizes the importance of testing and best practices to ensure a user-friendly experience for a global audience.

Uploaded by

dungnq222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PRACTICE EXERCISE 4: LOCALIZATION 57

PRACTICE EXERCISE 4: LOCALIZATION

LEARNING OBJECTIVES

In this lesson, we will learn about Localization in mobile programming:


− Implement localization features in Android applications using resource
files, such as strings.xml for managing text resources in multiple
languages.
− Modify and expand existing applications to support localization,
adapting user interface elements and content to cater to diverse
language preferences.
− Troubleshoot common localization issues and errors, including handling
text expansion and contraction, maintaining consistent layouts, and
ensuring accurate translations.
− Apply acquired knowledge and skills to real-world Android
development projects, incorporating localization as an integral part of
the development workflow to create inclusive and user-friendly
applications for a global audience

OVERVIEW

Localization in Android development is the process of adapting your app to


support multiple languages and regions. This enhances accessibility and user
experience globally. By using string resource files and the Locale class, you can easily
translate text, adjust layouts, and format data for different languages and regions.
Thorough testing and adherence to best practices are crucial to ensure your app
functions accurately and smoothly across platforms. Effectively implementing
localization enables you to create inclusive and user-friendly apps for users
worldwide.

TERMINOLOGY

Localization: The process of adapting an application to support multiple


languages and regions, ensuring that it is culturally and linguistically appropriate for
users worldwide.
58 PRACTICE EXERCISE 4: LOCALIZATION

Internationalization: The design and development process of making an


application capable of being adapted to different languages and regions without
engineering changes.
String resource: A text string that is externalized from the code and stored in
resource files, allowing for easy translation and modification without altering the
source code.
Locale: A specific geographical, political, or cultural region. In Android, locales
are represented by objects of the Locale class and are used to specify language and
region preferences.
Language: The primary spoken or written form of communication used by
humans. In the context of Android localization, it refers to the preferred language
of the user interface and content.
Region: A specific geographical area or territory. In Android localization,
regions are used to specify cultural preferences such as date formats, currency
symbols, and measurement units.
String resource file: An XML file that contains string resources for a specific
language or region. These files are stored in the res/values directory of an Android
project and are named according to the language or region they represent (e.g.,
strings.xml).
Context: An object that provides access to application-specific resources and
information about the application's environment. It is typically used to retrieve
localized resources such as strings, images, and layout files.
String resource ID: A unique identifier assigned to each string resource in an
Android project. It is used to reference the string resource in code and XML files.
Plurals: Different forms of a string resource that vary based on quantity, such
as singular and plural forms. Plurals are used to handle grammatical differences in
languages with singular, dual, and plural forms.
Formatting: The process of adjusting the appearance or structure of text,
numbers, dates, or other data to meet specific requirements or conventions, such
as language-specific formats or cultural preferences.
Date format: The arrangement of date components (e.g., day, month, year) in
a specific order, as determined by regional conventions and preferences.
Time format: The arrangement of time components (e.g., hours, minutes,
seconds) in a specific order, as determined by regional conventions and preferences.
PRACTICE EXERCISE 4: LOCALIZATION 59

Number format: The representation of numerical values, including decimal


places, thousand separators, and currency symbols, according to regional
conventions and preferences.
Layout direction: The direction in which content is displayed on the screen,
such as left-to-right (LTR) or right-to-left (RTL), which is determined by the language
and script being used. It is important for supporting languages that are written from
right to left, such as Arabic and Hebrew.
4.1. THEORETICAL SUMMARY
4.1.1. Knowledge
To perform localization in Android app development, you need the following
knowledge: Java or Kotlin:
Understanding of Java or Kotlin programming languages is essential for
Android app development. These are the primary programming languages used for
Android development.
XML: Knowledge of XML is necessary for working with resource files in Android,
such as string resource files (strings.xml) used to store text strings used in the app.
Resource Management: Understanding how to manage resources in an
Android project, including creating and using string resources, images, sounds, and
layout structures.
Locale and Resource Configuration: Understanding how to use Locale to
represent geographical regions and how to configure resources for each language
and region in Android.
Localization Process: Familiarity with the basic process of implementing
localization in an Android project, including translating text strings, adjusting layout
structures, and formatting dates, numbers according to the requirements of each
language and region.
Localization Testing: Knowing how to test the app after implementing
localization to ensure it functions correctly and displays accurately in all languages
and regions.
Best Practices in Localization: Applying best practices in localization, including
using placeholders, managing resources efficiently, and testing translation quality.
60 PRACTICE EXERCISE 4: LOCALIZATION

4.1.2. Skill
To effectively implement localization in Android app development, you need
the following skills:
Programming Skills (Java/Kotlin): Proficiency in Java or Kotlin programming
languages is essential for Android development. You should be able to write clean,
efficient, and maintainable code to implement localization features.
Understanding of Android Framework: Familiarity with the Android
framework, including activities, fragments, views, resources, and resource
management, is necessary to integrate localization into your app seamlessly.
XML: Knowledge of XML is crucial for defining layouts, storing string resources,
and configuring other resources in your Android app.
Locale and Resource Configuration: Understanding how to work with Locale
and configure resources for different languages and regions is essential for
implementing localization effectively.
User Interface Design: Proficiency in designing user interfaces that adapt well
to different screen sizes, orientations, and languages is important for providing a
consistent and intuitive user experience across all localized versions of your app.
4.2. BASIC PRACTICE
4.2.1. Exercise 1
Create a small Android application to implement the basic functionality of switching
to another location (region) within the app, using string and image resources. This
project allows users to select a location from a predefined list and switch the app's
location accordingly. When the location is changed, all location-related elements
within the app, such as messages, images, and localized content, will be updated
accordingly:
1. The app should have a simple user interface with a button or dropdown
menu to select the location.
2. Support at least two different locations (regions), such as the United
States and another country of your choice.
3. When the user selects a location, the app's location should switch
immediately, and all location-related elements within the app should be
updated accordingly, including messages and images.
PRACTICE EXERCISE 4: LOCALIZATION 61

4. Use string resources to store messages and image resources to store


images or icons representing each location.
5. Include clear instructions or guidance for users on how to switch
between locations.
6. Ensure that the selected location persists across app sessions, so the
chosen location remains active even after the app is closed and
reopened.
7. Optionally, you can add visual indicators or effects to enhance the
experience when switching between locations.
Please follow these steps:
Step 1: Create a New Project in Android Studio
Create new project with name : labfour. (Should choose Empty Activity, don’t need
to use other layout temmplates). Choose Java for main programming language
Click on “Help me choose” to consider what exactly android version that you
want to make default minimum. Now, Lastest android version is Android S – 12, if
you choose Android S, your application only run on 13.5% of devices on the world.
In this option, your application not cover all marketplay if you release for users.

Next, Prepare your Virtual Device (Turn on it). Delete default component
(TextView - “Hello world!”): Click on it and press Delete button on your keyboard.
Then, Right click on ConstraintLayout => Convert view…=> LinearLayout
62 PRACTICE EXERCISE 4: LOCALIZATION

Next, insert your Image into app→ res →drawable → Ho-Chi-Minh-City.jpg

Next, Create string resource with English version (default strings.xml) and
French version (values-fr > strings.xml). Switch to Project mode => Right click to res
folder => New => Android Resource Directory

Next, right click to “values-fr” folder => New => Values resource file => Type:
“strings”=> OK
PRACTICE EXERCISE 4: LOCALIZATION 63

Customize two strings.xml files: Values→ strings.xml


<resources>
<string name="app_name">Ho Chi Minh city map</string>
<string name="welcome">Welcome to Ho Chi Minh city</string>
<string name="here">You are in District 1</string>
<string name="locate">Locate</string>
</resources>

Values-fr→ strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Carte de Ho Chi Minh-ville</string>
<string name="welcome">Bienvenue à Hô-Chi-Minh-Ville</string>
<string name="here">Vous êtes dans le District 1</string>
<string name="locate">Localiser</string>
</resources>

Step 2: UX/UI Design


Design your layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="49dp"
android:gravity="center"
android:text="@string/welcome"
android:textAlignment="center"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="282dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/ho_chi_minh_city" />
<TextView
64 PRACTICE EXERCISE 4: LOCALIZATION

android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/here" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/locate"
android:textStyle="bold" />

</LinearLayout>

Next, create new folder: layout-land. This folder have a layout file that serve
for landscape mode.

Next, create new layout file name must to be “activity_main.xml” – the same
name with portrait mode layout (activity_main.xml in “layout” folder)
PRACTICE EXERCISE 4: LOCALIZATION 65

Next, design your landscape layout: Youcan design any style of UI, put more
component in Lanscape mode.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="-9dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="2dp"
android:gravity="center"
android:text="@string/welcome"
android:textAlignment="center"
android:textSize="20sp" />
66 PRACTICE EXERCISE 4: LOCALIZATION
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="282dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="245dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="65dp"
android:scaleType="centerCrop"
android:src="@drawable/ho_chi_minh_city" />

<TextView
android:id="@+id/textView2"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="22dp"
android:layout_marginTop="102dp"
android:text="@string/here" />

<Button
android:id="@+id/button"
android:layout_width="221dp"
android:layout_height="94dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="171dp"
android:text="@string/locate"
android:textStyle="bold" />
</RelativeLayout>

Run your app with portrait orientation and landscape orientation: Turn on
Auto-rotate; Click rotate left/right
PRACTICE EXERCISE 4: LOCALIZATION 67

Open Setting in Virtual device => System => Languages & input => Languages
=> Francais (France) Move France to the first. Run app again and make sure your
app change to Fr language.

4.2.2. Exercise 2
Create an Android application that allows users to select a language from a list
and convert all the text within the application to the selected language.
Below is the step-by-step guide on how to create an Android app that allows
users to select a language from a list and converts all the text in the app to the
selected language:
Step 1: Create a New Project:
− Open Android Studio and create a new project.
− Choose "Empty Activity" as the template for your project.
Step 2: Add Languages to the Project:
− Navigate to the res folder in your project's browser.
− Right-click on the res folder and select "New" -> "Android Resource
Directory".
− Choose "values" from the resource type list and click "OK".
− Create a new directory inside values by right-clicking on the values
folder and selecting "New" -> "Values resource file".
− Name this file strings.xml.
Step 3: Define Strings for Each Language:
− Open the strings.xml file in each values folder and define the strings in
the corresponding language.
− For example, in values/strings.xml, you can define strings for the default
language (e.g., English).
− Similarly, you can create values-xx folders for other languages and
define strings in them.
68 PRACTICE EXERCISE 4: LOCALIZATION

Step 4: Create the User Interface:


− Open the activity_main.xml file in the res/layout folder.
− Add a Spinner and a TextView to display the selected language.
− The Spinner will contain a list of languages that the user can choose
from.
Step 5: Handle Language Selection Event:
− In the MainActivity.java file, add code to handle the event when the
user selects a language from the Spinner.
− When a language is selected, update all the text in the app to reflect the
selected language.
Step 6: Test the App:
− Run the app on an Android device or emulator.
− Select different languages from the Spinner to see if the text in the app
is converted to the new language.
Below is a simple source code example to implement the above steps:
strings.xml (values/strings.xml):
<resources>
<string name="app_name">LanguageSwitchApp</string>
<string name="select_language">Select Language:</string>
<string name="english">English</string>
<string name="spanish">Spanish</string>
</resources>

activity_main.xml (res/layout/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:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/languageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select_language"
android:textSize="18sp" />
<Spinner
android:id="@+id/languageSpinner"
android:layout_width="match_parent"
PRACTICE EXERCISE 4: LOCALIZATION 69

android:layout_height="wrap_content"
android:layout_below="@id/languageTextView"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/sampleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/languageSpinner"
android:layout_marginTop="16dp"
android:text="Hello, World!" />
</RelativeLayout>

MainActivity.java (java/com.example.languageswitchapp/MainActivity.java):
package com.example.languageswitchapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Spinner languageSpinner;


private TextView sampleTextView;

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

languageSpinner = findViewById(R.id.languageSpinner);
sampleTextView = findViewById(R.id.sampleTextView);

ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.languages_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
languageSpinner.setAdapter(adapter);

languageSpinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long l) {
switchLanguage(position);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// Do nothing
70 PRACTICE EXERCISE 4: LOCALIZATION

}
});
}
private void switchLanguage(int position) {
switch (position) {
case 0:
sampleTextView.setText(R.string.hello_world);
break;
case 1:
sampleTextView.setText(R.string.hola_mundo);
break;
}
}
}

4.3. APPLICATION EXERCISES


You can further practice Localization by completing the following exercises:
1. Create an Android application that integrates the Google Translate API
to translate text into the user's chosen language. Users can input text
to be translated and select the target language, and the application will
display the translated text.
2. Create an Android application that allows users to select one of the 63
provinces and cities of Vietnam. The application will display a list of
tourist attractions and detailed information about each tourist
attraction.
REFERENCES
[1]. Google. (n.d.). Android Developers Guide: Localization. Retrieved from
https://developer.android.com/guide/topics/resources/localization
[2]. Kharchenko, V. (2013). Localization Essentials. Android Authority. Retrieved
from https://www.androidauthority.com/localization-essentials-android-
apps-171382/
[3]. Marques, J. (2018). Localization in Android. Ray Wenderlich. Retrieved from
https://www.raywenderlich.com/9159-localization-in-android-tutorial
[4]. TutorialsPoint. (n.d.). Android Localization Tutorial. Retrieved from
https://www.tutorialspoint.com/android/android_localization.htm
[5]. Prathap, S. (2020). Localization for Android Apps. Medium. Retrieved from
https://medium.com/swlh/localization-for-android-apps-fa51ab8ba638

You might also like