Different Ways to Hide ActionBar in Android with Examples

23 Feb, 2021
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, or 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:
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: 
XML
<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>
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:
Java
import android.support.v7.app.AppCompatActivity; import android.view.View; import android.os.Bundle;  public class MainActivity extends AppCompatActivity {       @Override    protected void onCreate(Bundle savedInstanceState)     {         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 (getSupportActionBar() != null) {             getSupportActionBar().hide();         }     } }
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: 
Java
import android.support.v7.app.AppCompatActivity; import android.view.View; import android.os.Bundle;   public class MainActivity extends AppCompatActivity {       @Override    protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);           // set Windows Flags to Full Screen         // using setFlags function         getWindow().setFlags(             WindowManager.LayoutParams.FLAG_FULLSCREEN,             WindowManager.LayoutParams.FLAG_FULLSCREEN);         setContentView(R.layout.activity_main);     } }
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. 
Java
import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // try block to hide Action bar        try {            this.getSupportActionBar().hide();        }        // catch block to handle NullPointerException        catch (NullPointerException e) {        }    }}
Create an Android app and choose any one of the above approaches to hide the ActionBar.
Define the activity. Here we simply print “GeeksforGeeks” on the screen using a Text View in ConstraintLayout:
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.geeksforgeeks.gfg.MainActivity">       <TextView        android:id="@+id/textView"        android:layout_width="129dp"        android:layout_height="55dp"        android:text="Geeks For Geeks"        tools:layout_editor_absoluteX="148dp"        tools:layout_editor_absoluteY="306dp" />   </androidx.constraintlayout.widget.ConstraintLayout>
Copy the Java code as per the approach chosen from above.
Compile and run the app.
riyamathur
Android-Bars
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Navigation Drawer in Android
How to Create and Add Data to SQLite Database in Android?
Arrays in Java
Split() String method in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java
For-each loop in Java