Open In App

How to Hide NavigationBar in Android?

Last Updated : 27 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

NavigationBar in Android is a row comprising the back button, home button, and Recent button located at the bottom of the application. Most Android 5.0 Lollipop and above devices have no physical navigation buttons and hence come as buttons present on the screen throughout the screen life.

However, these buttons become unnecessary for applications involving games and videos. Moreover, accidental touch on these buttons can result in data loss, especially on applications that run on servers in real-time. So, in this article, we will show you how you could hide NavigationBar on Android.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
  
class MainActivity : AppCompatActivity() {
  
    @RequiresApi(Build.VERSION_CODES.R)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // A function to hide NavigationBar
        hideSystemUI()
    }
  
    // Function to hide NavigationBar
    @RequiresApi(Build.VERSION_CODES.R)
    private fun hideSystemUI() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window,
            window.decorView.findViewById(android.R.id.content)).let { controller ->
            controller.hide(WindowInsetsCompat.Type.systemBars())
              
            // When the screen is swiped up at the bottom 
            // of the application, the navigationBar shall
            // appear for some time
            controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
}


Output:

You can see that the NavigationBar is now hidden. However, we have programmed it such that when a swipe up is made at the bottom of the screen, NavigationBar will appear for a few seconds.

Before and after hiding the navigation bar



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads