How can i change color of action bar and its over flow menu?

129 views Asked by At

Here is a screenshot of my application: enter image description here

I can access buttons using R.id.button1 and change its background using

case R.id.lightgreen:
          for (Button currentButton : buttons) {
                currentButton.setBackgroundResource(R.drawable.lightgreen);
            }

Is there any way to access action bar and its overflow menu to set their color in same way?

[EDIT]

ActionBar actnbar;
actnbar.setBackgroundDrawable(R.drawable.blue_button);

this gives me error asking to convert blue_button to drawable but if i do that then i won't be able to set buttons.

2

There are 2 answers

3
Kenan Begić On

Define your custom style.xml in values folder of your project like this :

<resources>

    <style name="Theme.CustomTHeme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/Theme.CustomTHeme.ActionBar</item>
    </style>

    <style name="Theme.CustomTHeme.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/header_gradient</item>
        <item name="titleTextStyle">@style/Theme.CustomTHeme.ActionBar.TitleTextStyle</item>
    </style>

    <style name="Theme.CustomTHeme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#fff</item>
    </style>

</resources>

And then in your AndroidManifest.xml :

<application
        android:name=".App"
        android:label="@string/app_name"
        android:icon="@drawable/logo"
        android:theme="@style/Theme.CustomTHeme">
...

You also must take care of supporting other api-s (other values folders).

If you are using actionbarcompat support library you can access actionbar in your activity that extends ActionBarActivity with getSupportActionBar() and use setStackedBackgroundDrawable() or setBackgroundDrawable() or corresponding getActionBar() method if not using support library.

1
Mark Buikema On

The Action Bar Style Generator is a great tool to style your action bar.