Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lấy ngôn ngữ hiện tại của thiết bị Android

Giới thiệu

Hướng dẫn này trình bày cách lấy ngôn ngữ (locale) hiện tại đang được chọn trên thiết bị Android và hiển thị nó.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" />
</RelativeLayout>

Bước 3: Viết logic xử lý (src/MainActivity.java)

package app.com.sample;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView textView = findViewById(R.id.textView);
        
        // Lấy Locale mặc định của thiết bị
        Locale currentLocale = Locale.getDefault();
        
        // Hiển thị tên ngôn ngữ (ví dụ: "Tiếng Việt", "English", "日本語")
        String languageName = currentLocale.getDisplayLanguage();
        
        // Hoặc lấy mã ngôn ngữ (vi, en, ja, ...)
        String languageCode = currentLocale.getLanguage();
        
        // Lấy quốc gia/vùng (VN, US, JP, ...)
        String countryCode = currentLocale.getCountry();
        
        textView.setText("Ngôn ngữ thiết bị: " + languageName 
            + " (" + languageCode + "-" + countryCode + ")");
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Mở rộng: Hỗ trợ đa ngôn ngữ trong ứng dụng

1. Tạo file strings cho từng ngôn ngữ

  • res/values/strings.xml - Mặc định (English)
  • res/values-vi/strings.xml - Tiếng Việt
  • res/values-fr/strings.xml - Tiếng Pháp

2. Thay đổi ngôn ngữ ứng dụng theo chương trình

public void setAppLocale(String languageCode) {
    Locale locale = new Locale(languageCode);
    Locale.setDefault(locale);
    
    Configuration config = getResources().getConfiguration();
    config.setLocale(locale); // API 24+
    // config.locale = locale; // API cũ
    
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. TextView sẽ hiển thị ngôn ngữ hiện tại của thiết bị.

Cách lấy ngôn ngữ hiện tại của thiết bị Android