setSystemUiVisibility is deprecated. Deprecated in Java
you can also use like this:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars.
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
// You can hide the caption bar even when the other system bars are visible.
// To account for this, explicitly check the visibility of navigationBars()
// and statusBars() rather than checking the visibility of systemBars().
if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
binding.toggleFullscreenButton.setOnClickListener {
// Hide both the status bar and the navigation bar.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
} else {
binding.toggleFullscreenButton.setOnClickListener {
// Show both the status bar and the navigation bar.
windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
}
}
view.onApplyWindowInsets(windowInsets)
}
}
JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
...
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
// Configure the behavior of the hidden system bars.
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
getWindow().getDecorView().setOnApplyWindowInsetsListener((view, windowInsets) -> {
// You can hide the caption bar even when the other system bars are visible.
// To account for this, explicitly check the visibility of navigationBars()
// and statusBars() rather than checking the visibility of systemBars().
if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
binding.toggleFullscreenButton.setOnClickListener(v -> {
// Hide both the status bar and the navigation bar.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
});
} else {
binding.toggleFullscreenButton.setOnClickListener(v -> {
// Show both the status bar and the navigation bar.
windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
});
}
return view.onApplyWindowInsets(windowInsets);
});
}
Referances : https://developer.android.com/develop/ui/views/layout/immersive
https://memreaktas.blogspot.com/2024/03/how-to-hide-navigation-a-bar-in-Android-Kotlin.html