Programmatically change the background color of the Overflow Menu in the Toolbar

Asked

Viewed 686 times

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

3 answers

1

1


You can work on your style.xml

Android Api >= 21:

<style name="AppThemeLillipopOuSuperior" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
    </style>


<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
     <item name="android:popupBackground">sua cor aqui</item>
</style>

Android Api < 21

<resources>

    <style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
        <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
        <item name="android:actionBarStyle">@style/MyApp.ActionBar</item>
    </style>

    <!-- The beef: background color for Action Bar overflow menu -->
    <style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
    </style>

    <!-- Bonus: if you want to style whole Action Bar, not just the menu -->
    <style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
        <!-- Blue background color & black bottom border -->
        <item name="android:background">@drawable/blue_action_bar_background</item>
    </style>   

</resources>

In his manifest.xml:

<application
    android:theme="@style/MyAppActionBarTheme" 
    ... >

So to change programmatically you can check which api using Build.VERSION.SDK_INT:

public void onCreate(Bundle savedInstanceState) {

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setTheme(R.style.AppThemeLillipopOuSuperior);
    } else {
        setTheme(R.style.Theme);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
  • seamusd, how can I change from style.xml so that it is assumed in the correct API?

  • @Vitormendanha corrected the answer, putting the form you want programmatically. You first check which version of the api, and then you set the style.

  • @seamude, if applying the style as you told me, gives error in my code line "setSupportActionBar(Toolbar);"?

  • @Vitormendanha Put the oncreate code in your question so we can better understand how you are doing.

  • @seamude, I edited my question and put the initial code of my menu "onCreate".

  • @Vitormendanha but and the error, where is the error that is giving?

  • @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...

  • @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"

  • 1

    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.

  • In your mistake, note that you already give a suggestion to put your windowActionBar for true...

  • @seamude, I put the following two lines in the style and no longer error: <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>

Show 6 more comments

0

 setTheme(android.R.style.Theme); 
//antes de super.onCreate(savedInstanceState);();

If you need to switch in real time, restart Activity.

  • Mr_anderson, gives error code "setSupportActionBar(Toolbar);". If you do not put this line of code works, but I am without Toolbar?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.