Giới thiệu
Bài viết minh họa cách xoay màn hình (Portrait/Landscape) lập trình khi nhấn Button, dùng setRequestedOrientation() với ActivityInfo.
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio.
Bước 2: Giao diện 2 nút (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSetPortrait"
android:text="Chuyển dọc (Portrait)"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:layout_marginTop="62dp"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSetLandscape"
android:text="Chuyển ngang (Landscape)"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:layout_marginTop="62dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/buttonSetPortrait"/>
</RelativeLayout>
Bước 3: Logic xoay màn hình (src/MainActivity.java)
package app.com.sample;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonSetPortrait = findViewById(R.id.buttonSetPortrait);
Button buttonSetLandscape = findViewById(R.id.buttonSetLandscape);
buttonSetPortrait.setOnClickListener(v ->
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
);
buttonSetLandscape.setOnClickListener(v ->
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
);
}
}
Bước 4: 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>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Nhấn nút để xoay màn hình dọc/ngang theo ý muốn.


Các chế độ Orientation (ActivityInfo)
| Hằng số | Mô tả |
|---|---|
| SCREEN_ORIENTATION_PORTRAIT | Dọc (se로) |
| SCREEN_ORIENTATION_LANDSCAPE | Ngang (ngang) |
| SCREEN_ORIENTATION_SENSOR | Tự động theo cảm biến |
| SCREEN_ORIENTATION_USER | Theo cài đặt người dùng (auto rotate on/off) |
| SCREEN_ORIENTATION_FULL_SENSOR | Tất cả 4 hướng (kể cả ngược) |
| SCREEN_ORIENTATION_REVERSE_PORTRAIT | Dọc ngược (đầu xuống) |
| SCREEN_ORIENTATION_REVERSE_LANDSCAPE | Ngang ngược |
| SCREEN_ORIENTATION_LOCKED | Khóa hướng hiện tại (API 18+) |
| SCREEN_ORIENTATION_UNSPECIFIED | Mặc định hệ thống |
Cách khai báo trong Manifest (tĩnh)
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" /> <!-- hoặc landscape, sensor, user, fullSensor, locked -->
Lưu ý quan trọng
setRequestedOrientation() chỉ ảnh hưởng đến Activity hiện tại
- Gây recreate Activity (onDestroy → onCreate) → mất state nếu không save/restore
- Dùng
onSaveInstanceState() / ViewModel để giữ dữ liệu
- NếuManifest fix cứng
android:screenOrientation, code Java không thể override
- Tránh xoay liên tục trong code - gây nháy mắt, UX kém
- Video player, game thường dùng Landscape; Form nhập liệu dùng Portrait
Xử lý configuration change (không recreate)
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
android:screenOrientation="sensor">
// Trong Activity - không recreate, tự handle
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Cập nhật UI thủ công nếu cần
}
setRequestedOrientation() chỉ ảnh hưởng đến Activity hiện tạionSaveInstanceState() / ViewModel để giữ dữ liệuandroid:screenOrientation, code Java không thể override