1
My application has an option that allows the user to switch between layout daytime colours and other layout night colors. My problem is that I don’t know how to programmatically change the color of the text and the background color of the Overflow menu in Toolbar
(by file styles.xml
I know how to do it, but not programmatically)?`
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(R.style.AppTheme2);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabs);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
viewPager = (ViewPager) findViewById(R.id.container);
setSupportActionBar(toolbar);
}
Error message:
FATAL EXCEPTION: main
Process: info.no_ip.menda.pk_alerta_2, PID: 16365
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.no_ip.menda.pk_alerta_2/info.no_ip.menda.pk_alerta_2.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:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
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_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at info.no_ip.menda.pk_alerta_2.MainActivity.onCreate(MainActivity.java:173)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
10-24 13:28:22.531 865-986/system_process I/ActivityManager: Delay finish: com.google.android.gms/.gass.chimera.PackageChangeBroadcastReceiver
10-24 13:28:22.599 865-910/system_process D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
10-24 13:28:22.615 865-5410/system_process W/ActivityManager: Force finishing activity info.no_ip.menda.pk_alerta_2/.MainActivity
[ 10-24 13:28:22.715 865: 5410 D/ ]
seamusd, how can I change from style.xml so that it is assumed in the correct API?
– Vitor Mendanha
@Vitormendanha corrected the answer, putting the form you want programmatically. You first check which version of the api, and then you set the style.
– viana
@seamude, if applying the style as you told me, gives error in my code line "setSupportActionBar(Toolbar);"?
– Vitor Mendanha
@Vitormendanha Put the oncreate code in your question so we can better understand how you are doing.
– viana
@seamude, I edited my question and put the initial code of my menu "onCreate".
– Vitor Mendanha
@Vitormendanha but and the error, where is the error that is giving?
– viana
@seamude, I put the error in the question. If I comment on the line "setSupportActionBar(Toolbar);", your code works. The problem is that I need Toolbar in my project...
– Vitor Mendanha
@Vitormendanha is difficult to help you, because there is no way to see the other parts of your code. Try setting the theme in your Toolbar app:Theme="@style/Themeoverlay.AppCompat.Actionbar"
– viana
Take a look at this answer http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor It’s the same mistake that’s happening to you.
– viana
In your mistake, note that you already give a suggestion to put your
windowActionBar
fortrue
...– viana
@seamude, I put the following two lines in the style and no longer error: <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
– Vitor Mendanha