Mobile Device Programming Lab 04
Mobile Device Programming Lab 04
LEARNING OBJECTIVES
OVERVIEW
TERMINOLOGY
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
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, 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
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>
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
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;
@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;
}
}
}