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

Cách ẩn thanh điều hướng (Navigation Bar) và nút Home trong Android

Giới thiệu

Bài viết này hướng dẫn cách ẩn thanh điều hướng hệ thống (navigation bar - chứa nút Back, Home, Recent) và chuyển ứng dụng sang chế độ toàn màn hình (fullscreen/immersive mode) trong Android.

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

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

Khởi tạo project Android Studio mới.

Bước 2: Layout đơn giản (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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Ẩn Home và các nút hệ thống"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: Code Java ẩn Navigation Bar (src/MainActivity.java)

package app.com.sample;

import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
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();
        
        // Kết hợp các flag để ẩn hoàn toàn system UI
        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; // Quan trọng: giữ trạng thái immersive

        // Sử dụng Timer để liên tục áp dụng flags (xử lý trường hợp system UI tự hiện lại)
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> decorView.setSystemUiVisibility(uiOptions));
            }
        };
        // Chạy mỗi 2ms để đảm bảo luôn ẩn
        timer.scheduleAtFixedRate(task, 1, 2);
    }
}

Giải thích các Flag quan trọng:

  • SYSTEM_UI_FLAG_HIDE_NAVIGATION: Ẩn navigation bar
  • SYSTEM_UI_FLAG_FULLSCREEN: Ẩn status bar
  • SYSTEM_UI_FLAG_IMMERSIVE_STICKY: Giữ chế độ immersive, user swipe từ cạnh màn hình để hiện tạm thời
  • SYSTEM_UI_FLAG_LAYOUT_*: Cho phép layout vẽ đầy đủ màn hình (kể cả vùng system UI)

Bước 4: Cấu hình Manifest

<?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

Chạy app trên thiết bị thật. Navigation bar và Status bar sẽ bị ẩn. Vuốt từ cạnh màn hình (dưới hoặc trên) để hiện tạm thời system UI.

Cách ẩn thanh điều hướng (Navigation Bar) và nút Home trong Android

Cách hiện đại hơn (API 30+)

// Trong onCreate()
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat controller = ViewCompat.getWindowInsetsController(window.decorView)
controller?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller?.hide(WindowInsetsCompat.Type.systemBars())

Lưu ý quan trọng

  • Cần test trên thiết bị thật (emulator có thể hoạt động khác)
  • Timer chạy liên tục có thể tiêu tốn pin, cân nhắc dùng View.OnSystemUiVisibilityChangeListener để tối ưu
  • Một số thiết bị OEM có thể không hỗ trợ ẩn hoàn toàn navigation bar