[java] Android: how to hide ActionBar on certain activities

I've developed a simple demo application with a splash screen a map and some regular screens.

I have an action bar at the top that contains a logo. It all looks fine on my phone (Galaxy s1 I9000 V2.3) but when i test it on Galaxy s2 v4 the action bar appears also in the splash screen and in the map screen.

The spalsh and map activity are not even inheriting from ActionBarActivity so how is that possible and how can i make it go away?

Manifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name=".HomeActivity"
            android:icon="@drawable/android_logo"
            android:label=""
            android:logo="@drawable/android_logo" >

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>
        <activity
            android:name=".MapActivity"
            android:label="" >
        </activity>
        <activity
            android:name=".PackageActivity"
            android:icon="@drawable/android_logo"
            android:label=""
            android:logo="@drawable/android_logo" >
        </activity>
        <activity
            android:name=".SplashActivity"
            android:label="" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

MapActivity definition (it's a long one so i included just the definition):

public class MapActivity extends FragmentActivity implements LocationListener

Splash Activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class SplashActivity extends Activity{

    private static final long SPLASH_DISPLAY_LENGTH = 2000;

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

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashActivity.this,HomeActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);

    }

}

This question is related to java android android-actionbar

The answer is


To hide the ActionBar add this code into java file.

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

Add New Style in your style.xml

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

    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="LoginTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

and then add following line in your manifest

<activity android:name=".Login"
        android:label="@string/app_name"
        android:theme="@style/LoginTheme"
        >

You can use Low Profile mode See here

Just search for SYSTEM_UI_FLAG_LOW_PROFILE that also dims the navigation buttons if they are present of screen.


The above answers would help with the ActionBar thing. To add to it, use the following code in case you are using the Splash Screen: Use this before you set the content view:

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

Just to clarify, here's how you do it:

    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);         
    setContentView(R.layout.activity_main);

This would make your screen a full screen, i.e remove the top bar where you see the network bar, etc


If you were using Theme.AppCompat.Light, a better equivalent would be Theme.AppCompat.Light.NoActionBar.

I found that using Theme.AppCompat.NoTitleBar caused my button text to be invisible so I am using Theme.AppCompat.Light.NoActionBar.

<activity android:name=".Activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.AppCompat.Light.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Check for padding attribute if the issue still exists after changing theme.

Padding Issue creating a white space on top of fragment window / app window

Padding Issue creating a white space on top of fragment window / app window

Once you remove the padding - top value automatically the white space is removed.


As you are asking about how to hide in a certain activity, this is what you need :

getSupportActionBar().hide(); 

First cheek MainActivity for activity tag,Like -

public class MainActivity extends AppCompatActivity

Then goto menifest xml file and write-

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

in activity tag


1.Go to your manifest.xml file.

2.Find the activity tag for which you want to hide your ActionBar and then add ,

android:theme="@style/Theme.AppCompat.NoActionBar"

           <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            >

The ActionBar usually exists along Fragments so from the Activity you can hide it

getActionBar().hide();
getActionBar().show();

and from the Fragment you can do it

getActivity().getActionBar().hide();
getActivity().getActionBar().show();

Replace AndroidManifest.xml

android:theme="@style/FullscreenTheme"

to

android:theme="@style/Theme.Design.NoActionBar"

If you want to get full screen without actionBar and Title.

Add it in style.xml

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="windowActionBar">false</item>
      <item name="windowNoTitle">true</item>
      <item name="android:windowFullscreen">true</item>
</style>

and use the style at activity of manifest.xml.

<activity ....
      android:theme="@style/AppTheme.NoActionBar" > ......
</activity> 

Apply the following in your Theme for the Activity in AndroidManifest.xml:

<activity android:name=".DashboardActivity"
        android:theme="@style/AppFullScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Then Apply the following in your Style in style.xml

<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

This works in API 27

In the styles.xml replace code to the following....

<resources>
    <!-- No Action Bar -->
    <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>

Then in the files (eg. activity_list.xml) in which you do want to have a toolbar put the following code.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorPrimary"/>

If you have problems switch to linear layout (because that is what this code is tested on)


This works for me! Put this in your Activity in manifests

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

You can also make your custom theme in styles.xml like this:

<style name="Theme.MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name = "android:windowActionBar">false</item>
        <item name = "android:windowNoTitle">true</item>
        // more custom...
    </style>

Hope this help.


For selectively hiding Actionbar in activities:

Add the following code to onCreate (preferably on the last line of the function)

Kotlin:

supportActionBar?.hide()

Java:

getSupportActionBar().hide();


From here:

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.

So one option is to disable ActionBar completely (in the app manifest.xml file) and then add Toolbar in every page that needs an ActionBar.

(follow the above link for step-by-step explanation)


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-actionbar

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? Failed to load AppCompat ActionBar with unknown error in android studio Android transparent status bar and actionbar ActionBarActivity is deprecated Manage toolbar's navigation and back button from fragment in android setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error How to use SearchView in Toolbar Android How to get Toolbar from fragment? Display Back Arrow on Toolbar Remove title in Toolbar in appcompat-v7