How to hide Action Bar?

In this article, We will see how to hide action bar in android app.

asif ali
CodixLab
3 min readFeb 11, 2020

--

The action bar is an important part of the android design element. It provides consistent familiar look across the devices, easy navigation and view switching with tabs and dropdowns and also provides functionality to indicate user’s location in the app.

Although, Action Bar is good and an important feature for android applications, but sometimes we have to hide it in some scenarios. I’ll teach you how to hide the Action Bar. There are several ways to hide the Action Bar and I’ll discuss all possible ways to achieve this.

1) Hide Action Bar permanently from styles.xml

If you want to hide Action Bar throughout the app (Activities, Fragments) everywhere, you can simply navigate to res > values > styles.xml and change your Base application theme as mentioned below.

<resources> 
<! - Base application theme. →
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<! - Customize your theme here. →
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

2) Hide Action Bar for a particular activity:

You can directly hide Action Bar for a specific activity by specifying the NoActionBar theme in its activity tag in the manifest file. For this simply navigate to your projects’ root and open the Android manifest file and add a single line of code as mentioned below.

<activity android:name=".activity.ContinueLoginActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:screenOrientation="portrait" />

3) Hide Action Bar with custom Theme:

Sometimes you want to style your app in a custom way, and there is a requirement to hide the Action Bar. A simple and convenient method is to create another style extending the custom theme and add two items to the theme.

<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>

Example: If you want to use a theme called Mytheme, You have to create another theme called NoActionBar and add these two lines as follows:

<style name="NoActionBar" parent="MyTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>

4) Hide Action Bar Programmatically:

We have discussed methods to hide the Action Bar without touching our actual classes as it worked well. However, we can also do the same by adding a few lines of code in our activity class. Let me show you how we can do this from our Activity class. So in our Activity’s onCreate method, you can add the following lines of code.

Kotlin:

supportActionBar?.hide()

Java:

if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}

4) Hide Action Bar using Window Manager

You can set the WindowManager flag alternatively. This approach makes a lot easier to hide or show the Action Bar as a user interacts with your app. Again in your Activity’s onCreate method just add these lines.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

Keep in mind you have to specify these particular lines before setContentView() or your app will crash.

That’s it for these tutorials. As we have discussed different approaches to achieve this task and all of the discussed approaches are valid you can choose whatever approach you want to according to your needs.

Thanks..!!!

Originally published at https://codixlab.com/ 20/feburary/2020

--

--

asif ali
CodixLab

Full Stack Mobile App Developer — Flutter | Android | Laravel | Node JS | Technical Writer | Open Source Contributor