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

1

My application has an option that allows the user to switch between a layout of daytime colors and another layout of night colors. My problem is that I do not know how to programmatically change the text color and background color of the Overflow Menu in Toolbar (by styles.xml file I know how to do, but programmatically not)? '

@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/         ]
    
asked by anonymous 24.10.2016 / 16:53

3 answers

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>

On your manifest.xml :

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

So, to change programmatically you can check out the 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);
}
    
24.10.2016 / 18:25
1

If you want to switch at run time according to the color of some image that the user clicked you can use the Palette class of Android.

Class references: link

Usage references: link

    
25.10.2016 / 13:00
0
 setTheme(android.R.style.Theme); 
//antes de super.onCreate(savedInstanceState);();

If it is necessary to change in real time, restart the activity.

    
24.10.2016 / 18:23