ภาพรวม
ตัวอย่างนี้แสดงวิธีการซ่อน Navigation Bar และ Status Bar (Immersive Mode) เพื่อให้แอปใช้พื้นที่หน้าจอเต็มที่
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Disable Home and Other System buttons"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)
package com.example.immersivemode;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideNavigationBar();
}
}
private void hideNavigationBar() {
final View decorView = getWindow().getDecorView();
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
// ใช้ Timer เพื่อบังคับซ่อน Navigation Bar ตลอดเวลา
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(() -> decorView.setSystemUiVisibility(uiOptions));
}
};
timer.scheduleAtFixedRate(task, 1, 2);
}
}
ขั้นตอนที่ 4: ตั้งค่า Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.immersivemode">
<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>
การทดสอบ
รันแอปพลิเคชันบนอุปกรณ์จริง Navigation Bar และ Status Bar จะถูกซ่อนอัตโนมัติ สไลด์จากขอบหน้าจอเพื่อแสดงชั่วคราว

คำเตือนสำคัญ
- วิธีนี้ใช้
SYSTEM_UI_FLAG_IMMERSIVE_STICKY(API 19+) ซึ่งเป็นวิธีมาตรฐาน - การใช้ Timer ทุก 2ms อาจส่งผลต่อแบตเตอรี่ ควรพิจารณาใช้
View.OnSystemUiVisibilityChangeListenerแทน - ไม่สามารถ "ปิดใช้งาน" ปุ่ม Home จริงๆ ได้ (เป็นปุ่มฮาร์ดแวร์/ระบบ) แต่สามารถซ่อน Navigation Bar ได้