591

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
tyczj
  • 71,600
  • 54
  • 194
  • 296

27 Answers27

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>
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
Nadir Belhaj
  • 11,953
  • 2
  • 22
  • 21
  • 19
    is `windowActionBar` not the same as `android:windowActionBar`? – tyczj Oct 22 '14 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 '14 at 05:08
  • 5
    When I added that line in my styles the app crash :( – rkmax May 09 '15 at 21:28
  • 9
    Also add true If you're using AppCompatActivity after 22.1 support library http://stackoverflow.com/a/29853299/1402525 – Nick Nov 03 '15 at 03:06
  • This works very well `false true` – blueware Nov 14 '16 at 09:18
  • 8
    Don't forget to add: android:theme="@style/AppTheme" to application tag in manifest file. – live-love Jan 08 '18 at 17:25
  • I'd also like to add that in my case I had called `setContentView();` twice, stupid I know :). Which frustrated me almost enough to make me mad. – Abbas Mar 25 '19 at 17:44
  • Also, I added those lines in values/themes.xml and it worked! Thank you Nadir Belhaj – Konstantin F Dec 12 '20 at 23:49
  • It's from the styles and manifest files. This link is easy to fix it https://www.examtray.com/android/how-solve-android-exception-activity-action-bar-supplied-window-decor – ikmazameti Apr 10 '21 at 01:16
  • Happy it helped all of you – Nadir Belhaj Jul 25 '23 at 17:01
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>
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • 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 '14 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 – MrEngineer13 Oct 22 '14 at 21:07
  • 6
    I find this solution more elegant than @Nadir Belhaj's one, though both of them are correct. – voghDev Feb 12 '15 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 '16 at 19:11
  • In that case you cannot use Theme.AppCompat.Light.DarkActionBar which actually is a useful parent. – f470071 Mar 30 '17 at 02:07
152

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>
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
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.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
V_J
  • 1,081
  • 13
  • 33
  • 1
    Thanks! I forgot to add ` – CoolMind Dec 15 '17 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 in the layout file and that did it! . Thanks – Andrew Irwin Feb 10 '18 at 00: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

Nasz Njoka Sr.
  • 1,138
  • 16
  • 27
  • 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 08 '23 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>
Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
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.

Prabhakaran
  • 379
  • 3
  • 7
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.

Panther
  • 3,312
  • 9
  • 27
  • 50
S HemaNandhini
  • 333
  • 3
  • 5
7

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

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

A J
  • 4,542
  • 5
  • 50
  • 80
7

Or Simply change the @Style/AppTheme

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

This works for me!

Gaurav Bhagat
  • 97
  • 1
  • 5
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>
Asim Roy
  • 9,915
  • 4
  • 28
  • 40
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>
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Syed Hamza Hassan
  • 710
  • 1
  • 11
  • 24
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

Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
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
live-love
  • 48,840
  • 22
  • 240
  • 204
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.

Harry .Naeem
  • 1,245
  • 3
  • 20
  • 33
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));

    }
yash sachdeva
  • 637
  • 5
  • 13
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>
John Joe
  • 12,412
  • 16
  • 70
  • 135
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>
Deepanshu
  • 133
  • 1
  • 3
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>
jay fegade
  • 83
  • 8
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>
live-love
  • 48,840
  • 22
  • 240
  • 204
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.

live-love
  • 48,840
  • 22
  • 240
  • 204
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.

Michael Konz
  • 503
  • 1
  • 3
  • 20
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

Sandeep Dixit
  • 799
  • 7
  • 12
  • 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 08 '23 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.

Gleichmut
  • 5,953
  • 5
  • 24
  • 32
-2

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

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

Md. Ibrahim
  • 1,239
  • 14
  • 18
-3

I solved it by removing this line:

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

from activity properties in the Manifest file

Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
Flowra
  • 1,350
  • 2
  • 16
  • 19