0

I want to change an Android theme at run-time but i have problem:

I have created a new style

<style name="AppThemeRed" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimaryRed</item>
    <item name="colorPrimaryDark">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccentRed</item>
</style>

and tried to set it with setTheme(R.style.AppThemeRed); before setContentView(R.Layout.activity_main); but program closes unfortunately. this is the log:

04-01 22:10:09.850 3631-3631/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.shahinsoft.snote, PID: 3631
    Theme: themes:{}
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shahinsoft.snote/com.shahinsoft.snote.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5461)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
        Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
            at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
            at com.shahinsoft.snote.MainActivity.onCreate(MainActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:6251)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5461) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117) 

By the way, I'm rookie and learning android developing, so answer in simple words please.
Thanks!

Christian Grabowski
  • 2,782
  • 3
  • 32
  • 57
Shayan D
  • 620
  • 3
  • 14
  • 1
    Found a solution here http://stackoverflow.com/a/18301723/3061577 Hope it helps! Anyway what colors exactly do you want to change with the theme change? – Gerrerth Apr 01 '16 at 19:03

1 Answers1

0

The theme you are trying to set has its own Toolbar. For you to get this exception it means that in your layout you already specify a Toolbar view and you are setting it somewhere in your activity. If you do that, make sure to extend one of the .NoActionBar themes i.e. <style name="AppThemeRed" parent="Theme.AppCompat.NoActionBar">.

You might face a new problem though then as there is no "Light background - Dark No toolbar" theme in the AppCompat so you would have to style the toolbar yourself.

You can see an example how to switch themes on Runtime and also see how to style the toolbar in case you have more issues here:

Sample project

Theme declaration

Alex Styl
  • 3,982
  • 2
  • 28
  • 47