Giới thiệu
Bài viết hướng dẫn cách phát hiện orientation (xoay màn hình) của Activity: Portrait (dọc) hay Landscape (ngang) bằng cách override onConfigurationChanged().
Các bước thực hiện
Bước 1: Tạo project mới
Android Studio → File → New Project → Empty Activity.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvOrientation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bước 3: Xử lý orientation change (MainActivity.kt)
package com.example.checkorientation
import android.content.res.Configuration
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
updateOrientationText()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
updateOrientationText()
}
private fun updateOrientationText() {
val orientation = resources.configuration.orientation
tvOrientation.text = when (orientation) {
Configuration.ORIENTATION_PORTRAIT -> "Đang ở chế độ PORTRAIT (dọc)"
Configuration.ORIENTATION_LANDSCAPE -> "Đang ở chế độ LANDSCAPE (ngang)"
else -> "Không xác định"
}
}
}
Bước 4: Cấu hình Manifest (AndroidManifest.xml)
Quan trọng: Phải khai báo android:configChanges để Activity tự xử lý xoay màn hình thay vì bị destroy/recreate:
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Giải thích
onConfigurationChanged(): Được gọi khi cấu hình thay đổi (xoay màn hình, đổi ngôn ngữ, v.v.) nếu đã khai báo configChanges.Configuration.ORIENTATION_PORTRAIT: Màn hình dọc (cao > rộng).Configuration.ORIENTATION_LANDSCAPE: Màn hình ngang (rộng > cao).android:configChanges="orientation|screenSize": Bắt buộc từ API 13 (Android 3.2) phải có cả screenSize.
Lưu ý
- Nếu không khai báo configChanges, Activity sẽ bị destroy và tạo lại (onCreate chạy lại) khi xoay màn hình.
- Với Jetpack Compose, dùng
LocalConfiguration.current.orientation. - Trên thiết bị foldable/tablet, có thể có các trạng thái khác như
ORIENTATION_UNDEFINED.