Giới thiệu
Fullscreen Activity nghĩa là ẩn cả Status Bar (thanh trạng thái) và Navigation Bar (thanh điều hướng) để nội dung chiếm toàn bộ màn hình. Bài viết tổng hợp các cách từ cơ bản đến hiện đại.
Các phương pháp
Phương pháp 1: Theme NoActionBar + WindowInsetsController (API 30+, Android 11+) - Khuyến nghị
res/values/themes.xml:
<style name="Theme.Fullscreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
MainActivity.kt:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Phải gọi trước setContentView
WindowCompat.setDecorFitsSystemWindows(window, false)
setContentView(R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.apply {
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
}
Phương pháp 2: Flags cũ (API 16-29)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.hide()
setContentView(R.layout.activity_main)
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
Phương pháp 3: Manifest (cũ, ít dùng)
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
Layout ví dụ (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#000000"
android:fitsSystemWindows="false"> <!-- Quan trọng! -->
<DigitalClock
android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:textColor="#FFFFFF" />
</LinearLayout>
Lưu ý quan trọng
- android:fitsSystemWindows="false" trên root view để nội dung vẽ full màn hình.
- WindowCompat.setDecorFitsSystemWindows(window, false) thay thế cho
fitsSystemWindowstrên API 30+. - Immersive sticky mode cho phép vuốt từ mép để hiện thanh hệ thống tạm thời.
- Tránh dùng
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"(deprecated).