ActionBar is a primary toolbar within the activity that may display the activity title, application-level navigation affordances, and other interactive items. Although Action Bar is an important feature for android applications, sometimes we have the need to hide it in either the entire app, some particular activity, or during some particular work. This article explains and demonstrates various ways to hide the ActionBar in an Android Application. There are various ways to hide Action Bar, demonstrated below:
Different ways to Hide ActionBar
1. Hide ActionBar from the entire App using styles.xml
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles.xml and change the base application to “Theme.AppCompat.Light.NoActionBar“. Below is the code snippet for this method and changes are made to styles.xml:
2. Hide ActionBar from any particular activity using Java code
If you want to hide Action Bar from particular activity just add few lines of code in the MainActivity.java file as mentioned in the code snippet below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Take instance of Action Bar
// using getSupportActionBar and
// if it is not Null
// then call hide function
if(supportActionBar != null) {
supportActionBar!!.hide()
}
}
}
// This code is contributed by Ujjwal KUmar Bhardwaj
3. Hide ActionBar while user interaction using WindowManager
Another way to hide Action Bar is through Window Manager by setting the WindowManager flag. This approach makes it a lot easier to hide the Action Bar when the user interacts with your application. You can use the “setFlags” function as described below in the code. Also, you have to use flags before setContentView() of Activity. Here is the java file to hide Action Bar:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// set Windows Flags to Full Screen
// using setFlags function
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
setContentView(R.layout.activity_main)
}
}
4. Hide ActionBar from any particular activity using try-catch
If you want to hide Action Bar from particular activity using try-catch blocks just add few lines of code in app > java > package name > MainActivity.java file as mentioned in the code snippet below. Comments are added inside the code to understand the code in more detail.