Reputation: 21
I wrote an app in Kotlin that overlays a black color over the entire screen. The app works, however I can still see the top and bottom status bars. I tried to make it full screen by adding these lines in the MainActivity.kt
import android.view.Window
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
private fun setFullScreen() {
window.setDecorFitsSystemWindows(false)
val controller = window.insetsController
controller?.let {
it.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
it.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE)
}
}
}
as explained on this link
https://techharvestbd.wordpress.com/2021/09/28/make-full-screen-in-android-programmatically/
But I get this error:
Unresolved reference: WindowInsets
How can I do immersive mode in Kotlin?
Upvotes: 1
Views: 32
Reputation: 119
Android provides an official way to achieve full-screen immersive mode using enableEdgeToEdge
. You should call this method before setContentView()
in your activity. This approach is recommended as it is reliable and officially supported.
Additionally, if your content needs to extend under the status and navigation bars, you should handle insets properly. You can apply inset padding to your root view using:
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
For a complete guide on hiding system bars and implementing immersive mode, refer to the official documentation:
š Immersive Mode Guide
enableEdgeToEdge()
before setContentView()
.val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
// Hide the system bars.
windowInsetsController.hide(Type.systemBars())
// Show the system bars.
windowInsetsController.show(Type.systemBars())
Upvotes: 0