595

Trying to move over my stuff to use Toolbar instead of action bar but I keep getting an error saying

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.weddingalbum/com.xxx.xxx.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165)
            at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
            at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:113)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

so then I added in my style for my activity to have no actionbar

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

and the theme is applies to activties in my manifest

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/>

MainActivity extends GooglePlayServiceActivity so I also set the theme there too

<activity
       android:name=".GooglePlayServicesActivity"
       android:label="@string/title_activity_google_play_services"
       android:theme="@style/AppCompatTheme">

but I still get the error. I also do not request window feature anywhere. any ideas why I still get this?

27 Answers 27

957

I think you're developing for Android Lollipop, but anyway include this line:

<item name="windowActionBar">false</item> 

to your theme declaration inside of your app/src/main/res/values/styles.xml.

Also, if you're using AppCompatActivity support library of version 22.1 or greater, add this line:

<item name="windowNoTitle">true</item>

Your theme declaration may look like this after all these additions:

<!-- 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>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
10
  • 19
    is windowActionBar not the same as android:windowActionBar?
    – tyczj
    Oct 22, 2014 at 19:07
  • 68
    It is, but AppCompat stuff looks at the app namespace attribute not the android one. This makes it possible to specify the attribute even in platforms that didn't originally have it. It's how it allows for backporting of things like colorPrimary.
    – yarian
    Oct 24, 2014 at 5:08
  • 5
    When I added that line in my styles the app crash :(
    – rkmax
    May 9, 2015 at 21:28
  • 9
    Also add <item name="windowNoTitle">true</item> If you're using AppCompatActivity after 22.1 support library stackoverflow.com/a/29853299/1402525
    – Nick
    Nov 3, 2015 at 3:06
  • 8
    Don't forget to add: android:theme="@style/AppTheme" to application tag in manifest file.
    – live-love
    Jan 8, 2018 at 17:25
221

Another easy way is to make your theme a child of Theme.AppCompat.Light.NoActionBar like so:

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
     ...
</style>
5
  • I tried this method too but I still got the error. However that could have been before I set the theme to my GooglePlayServiceActivity, I dont remember
    – tyczj
    Oct 22, 2014 at 21:00
  • 2
    Yea, this is what I've been using. Just make sure that the activity is using this theme in the manifest Oct 22, 2014 at 21:07
  • 6
    I find this solution more elegant than @Nadir Belhaj's one, though both of them are correct.
    – voghDev
    Feb 12, 2015 at 14:34
  • I think this is the better answer, using the inline XML attribute (windowActionBar=false) didn't work - I got same error as @tyczj
    – Booger
    Oct 14, 2016 at 19:11
  • In that case you cannot use Theme.AppCompat.Light.DarkActionBar which actually is a useful parent.
    – f470071
    Mar 30, 2017 at 2:07
153

Add single line android:theme="@style/AppTheme.NoActionBar" to activity in AndroidManifest and you've done.


AndroidManifest.xml:

<activity android:name=".activity.YourActivity"
          android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

styles.xml

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>
0
57

To use Toolbar as an Action Bar, first disable the decor-provided Action Bar.

The easiest way is to have your theme extend from

Theme.AppCompat.NoActionBar

(or its light variant).

Second, create a Toolbar instance, usually via your layout XML:

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

Then in your Activity or Fragment, set the Toolbar to act as your Action Bar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

This code worked for me.

2
  • 1
    Thanks! I forgot to add <android.support.v7.widget.Toolbar to XML.
    – CoolMind
    Dec 15, 2017 at 13:37
  • 1
    I couldnt figure why my toolbar colour was just blank. Couldnt seem to figure out how to change it to green. I feel silly as all I had to do was put android:background=" " within the <android.support.v7.widget.Toolbar /> in the layout file and that did it! . Thanks Feb 10, 2018 at 0:34
24

If you want to combine some activities with actionbar and others not, you should use the base theme have actionbar enabled and then create a sub theme that you gonna use it on activities that don't require actionbar

For example you can use a sub style like this

             <style name="AppTheme.NoActionBar">
               <item name="windowActionBar">false</item>
               <item name="windowNoTitle">true</item>
            </style>

While the base theme extends say

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

and then use the non actionbar theme in the AndroidManifest File within the activity tag say

   <activity
        android:name="com.example.NonActionBarActivity"
        android:theme="@style/AppTheme.NoActionBar"

you must apply this to each individual activity that don't need actionbar so if your project requires fewer action bar activities than non, then it's better to apply this on the base theme level

1
  • Thanks! How can I have one custom toolbar use 2 different styles that I set dynamically in the activity, based on my desired mode in the app?
    – Skj
    Feb 8 at 14:34
14

I also faced same problem. But I used:

getSupportActionBar().hide();

before

setContentView(R.layout.activity_main);

Now it is working.

And we can try other option in Style.xml,

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

Add this in your values/styles.xml

<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

<style name="AppTheme" parent="AppBaseTheme">

</style>

And add the following code in your values-v11/styles.xml and values-v14/styles.xml

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

Thats it. It will work.

7

You need to change

  <activity
        android:name=".YOUR ACTIVITY"
        android:theme="@style/AppTheme.NoActionBar" />
</application>`

these lines in the manifest.It will perfectly work for me.

7

Use this inside your application tag in manifest or inside your Activity tag.

android:theme="@style/AppTheme.NoActionBar"

7

Or Simply change the @Style/AppTheme

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

This works for me!

1
  • This works pretty well if you want to use Toolbar than simply not use Action bar. Any idea why we cant use both at same time? Jul 15, 2021 at 10:32
6

Go to the 'style.xml' of your project and make windowActionBar to false

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>
2
  • 13
    How are you helping others when your answer is not correct in reguards to my question
    – tyczj
    Dec 22, 2014 at 13:07
  • Instead of using "Theme.AppCompat.Light", use "Theme.AppCompat.Light.NoActionBar"
    – Christina
    May 12, 2015 at 21:34
5

Add this two line in your app theme located in style.xml:-

 <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>

    //Add this two line will fix your problem
    <item name="windowNoTitle">true</item>   <--1(this applies after 22.0.1 support library i think 
    <item name="windowActionBar">true</item> <--2

5

If you are using Appcompact Activity use these three lines in your theme.

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

If you get the error on this line:

setSupportActionBar(...);

You need to check if your activity is referring to a theme that contains a toolbar. Your application's AppTheme might already contain a toolbar, such as

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

and you are trying to add a second one. If you want to use your application's AppTheme, you need to remove the theme from your activity, in the manifest.xml file.

For example:

<activity
    android:name=".ui.activities.SettingsActivity"
    android:theme="@style/AppTheme1" /> --> remove this theme
4

I had toolbar added in my xml. Then in my activity i was adding this statement:

setSupportActionBar(toolbar);

Removing this worked for me. I hope it helps someone.

2
  • What if I don't want to remove it? That is there are things that use the toolbar. Jul 3, 2018 at 6:57
  • I'm not sure about it, but i think removing this "setSupportActionBar(toolbar);" toolbar will stay there. Jul 4, 2018 at 7:13
3

Simply put, you can do the following:-

if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));

    }
3

This is how I solved the problem. Add below code in your AndroidMainfest.xml

<activity android:name=".YourClass"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
 </activity>
1
  • 1
    perfect! Thanks for sharing. Apr 29, 2019 at 7:07
3

Its very simple you just need to go with any one step out of two..

  1. change your device display mode from Dark mode to normal mode or .
  2. In style.xml if you are using AppCompat then add these 2 lines in your theme/style..<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
2
 <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:windowActionBar">false</item>
    </style>

    Change this to NoActionBar 

        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->

        </style>

    This one worked for me
2

try to change the parent of application theme

change below in themes.xml

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">

to

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

OR below is the correct way to disable the action bar in theme definition

    <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">    
    <item name="windowActionBar">false</item>
                <item name="windowNoTitle">true</item>
</style>
1

I got this error due to a custome attribute inside, removing it fixed the issue.

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
..
        <item name="searchBarBgColor">#383838</item>  --> remove this line
    </style>
1

It could also be because you have a style without a parent specified:

<style name="DarkModeTheme" >
    <item name="searchBarBgColor">#D6D6D6</item>
</style>

Remove it, and it should fix the problem.

1

I have my apps theme derived from Theme.Material3.DayNight.NoActionBar which has <item name="windowActionBar">false</item> and it works fine on all activities.

But as soon as I switch the device to dark mode I get the This Activity already has an action bar supplied by the window decor error.

I have to apply

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

to every activity.

I think this is strange, as the app theme is already defined without action bar. Couldn't yet figure out the reason for it.

0

A lot of answers given here are correct. My re-wording.

If you are explicitly adding appbar/actionbar to your app then you need to turn the default one off. That is if you want to use elements like

<com.google.android.material.appbar.AppBarLayout>
or 
<android.support.v7.widget.Toolbar>

then you need to turn off implicit action bar using correct style.

First thing is to look at the root theme of your application (the one mentioned in the androidmanifest.xml tag.

<application
       xer_main_round"
        android:supportsRtl="true"
        android:theme="@style/myRootTheme">
-----
then your style.xml or theme.xml or similar file should have style that turns off default actionbar. 

 <style name="myRootTheme" parent="Theme.AppCompat.Light.NoActionBar">

or you may use explicit style to hide it from the root theme.

<item name="windowActionBar">false</item> 

You may want to go through app-bar documentation https://developer.android.com/training/appbar/setting-up

1
  • Thanks! If I have 2 different styles for one custom toolbar, and I want to conditionally set either one of them based on the app mode, how would I do that at runtime?
    – Skj
    Feb 8 at 14:19
0

Your app should automatically support DayNight mode if you have this theme:

<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

However, if you supply a custom action bar via Toolbar in some of your layouts, you have to replace it to:

<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.NoActionBar">

Pay attention to you have values/themes.xml and night/themes.xml. You have to keep Theme.MaterialComponents.DayNight.NoActionBar in both this files.

-2

Just put parent="Theme.AppCompat.NoActionBar" in your style.xml file

Like - <style parent="Theme.AppCompat.NoActionBar" name="AppTheme.NoActionBar">

-3

I solved it by removing this line:

android:theme="@style/Theme.MyCompatTheme"

from activity properties in the Manifest file

Not the answer you're looking for? Browse other questions tagged or ask your own question.