[Học lập trình Android] Triển khai ứng dụng đa ngôn ngữ MultiLanguage

ngày 24-08-2017

Bài tập được trích ra từ chương trình học Lập trình viên Android tại Trung tâm Tin học
 
Sau khi thực hiện bài này người học có khả năng:
  • Triển khai ứng dụng đa ngôn ngữ
  • Xây dựng được giao diện login
  • Tuỳ biến theme của Activity
Yêu cầu: xây dựng ứng dụng có giao diện sau:
 
Triển khai ứng dụng đa ngôn ngữ  Triển khai ứng dụng đa ngôn ngữ  Triển khai ứng dụng đa ngôn ngữ
 
Ứng dụng sẽ thay đổi ngôn ngữ khi điện thoại thay đổi ngôn ngữ mặc định. Yêu cầu thực hiện thay đổi các ngôn ngữ như trên giao diện (Tiếng việt , Tiếng Anh, Tiếng đức) và một số ngôn ngữ khác như Spain, France, Japan
 
Gợi ý thực hiện:
 

 

Bước 1: Tạo Project đặt tên BT_Multilanguage

 

Bước 2: Xây dựng giao diện:

 

Bước 2.1 Xây dựng giao diện

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:background="@drawable/bg_gradient"
    tools:context="com.example.vudinhai.bt_multilanguage.MainActivity">
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:gravity="center"
        android:orientation="vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="40dp"
            android:text="@string/welcome"
            android:textColor="@color/white"
            android:textSize="45dp"
            android:textStyle="bold" />
 
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_form_rounded"
            android:orientation="vertical" >
 
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@null"
                android:hint="@string/email"
                android:padding="5dp"
                android:lines="1"/>
 
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:padding="5dp" />
        </LinearLayout>
 
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:background="@drawable/bg_button_rounded"
            android:text="@string/login"
            android:textColor="@color/white"/>
    </LinearLayout>
 
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/signup"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="25dp"
        android:textColor="@color/white"/>
</RelativeLayout>

 

Bước 2.2: Xây dựng tuỳ biến themes ( Values -> Styles.xml)

 

Bước 2.2.1: Thêm nội dung sau

 

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
 
</style>
 
 
Bước 2.2.2: Thay đổi AppTheme sang MyAppTheme trong AndroidManifrest:
 
 
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyAppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
 
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 
</application>
 

 

Bước 2.3: Xây dựng các file background trong thư mục drawable

 
Tạo các file drawable: bg_button_rounded, bg_form_rounded, bg_gradient trong drawble như sau:
Right chuột vào thư mục drawable chọn new -> Drawable resource file
 
Xấy dựng ứng dụng đa ngôn ngữ
 
 
bg_button_rounded.xml
 
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
 
    <!-- view background color -->
    <solid
        android:color="@color/bg_button_login" >
    </solid>
 
    <!-- If you want to add some padding -->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>
 
    <!-- Here is the corner radius -->
    <corners
        android:radius="6dp"   >
    </corners>
 
</shape>
 
bg_form_rounded.xml
 

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
 
    <!-- view background color -->
    <solid
        android:color="@color/white" >
    </solid>
 
    <!-- If you want to add some padding -->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>
 
    <!-- Here is the corner radius -->
    <corners
        android:radius="6dp"   >
    </corners>
 
</shape>
 
 
 
bg.gradient.xml
 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
 
    <gradient
        android:gradientRadius="750"
        android:endColor="@color/bg_gradient_end"
        android:startColor="@color/bg_gradient_start"
        android:type="radial" />
</shape>
 
Bước 3: Xây dựng strings.xml ngôn ngữ như sau:
 
 
Trường hợp xây dựng ngôn ngữ tiếng việt
Right chuột vào strings.xml -> new -> Values resource file
 
Xây dựng ứng dụng đa ngôn ngữ
 
 
filename : strings
Directory name: values-vi
Chọn OK để kết thúc:
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome">Xin chào</string>
    <string name="email">Thư điện tử</string>
    <string name="password">Mật khẩu</string>
    <string name="login">Đăng nhập</string>
    <string name="signup">Đăng ký tài khoản mới ?</string>
</resources>
 
 
Bước 4: Thay đổi ngôn ngữ mặc định điện thoại để kiểm tra kết quả
 
 
Vào setting -> Language&Input -> Language -> English(United state)
 
 
Chúc các bạn học tốt!
Trung tâm Tin học ĐH KHTN