5 Ways to Hide Android ActionBar

By:   –  Last updated:   –  #android ·  #java

Code Theme [Dark]

Content Overview [Hide]

5 methods to hide Android ActionBar permanently or temporarily in an Activity.

1 How to hide ActionBar permanently

1.1 Disabling ActionBar in current application's theme

Open app/res/vaules/styles.xml file, add an item to AppTheme style to disable ActionBar.

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- ... -->
        <item name="windowNoTitle">true</item>
    </style>
</resources>

disabling_android_actionbar

1.2 Applying a non-ActionBar theme to current application

Open res/vaules/styles.xml file, change the parent theme style to a non-ActionBar theme (which can be either your custom theme or Android system built-in theme).

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- ... -->
    </style>
</resources>

android_hide_actionbar_in_app_theme

2 How to hide ActionBar in an Activity

2.1 Applying a non-ActionBar theme to a specified Activity

Add a non-ActionBar theme to specified Activity in project's manifest file: app/manifests/AndroidManifest.xml. The added non-ActionBar theme can be either your custom theme or Android system built-in theme.

<!-- ... -->
<activity android:name=".MainActivity" 
          android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
<!-- ... -->

android_hide_actionbar_in_activity_theme

2.2 Hide ActionBar programmatically

Hide ActionBar directly in the onCreate method of Activity.

// MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

android_hide_actionbar_using_getSupportActionBar

2.3 Hide ActionBar using requestWindowFeature() API

// MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide ActionBar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);
    }
}

android_hide_actionbar_using_requestWindowFeature

It is noticeable that in this method MainActivity should inherit from Activity class instead of AppCompatActivity. Otherwise requestWindowFeature() will not work.

Additionally, requestWindowFeature() must be called before setContentView(), or the Activity will crash.

3 Video demo