24

I have added ActionBar tabs to my application. Default color for that underline is light blue. How do I change that color or style for selected tab ?

user533844
  • 2,053
  • 7
  • 36
  • 43

6 Answers6

98

For anyone wants to change actionbar color/background in code, you can do something like this

final ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_bg));

To change the tab bar color under the actionbar:

actionBar.setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_brown_dark)));

To change tab bar background:

actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
            R.drawable.coupon_header));
thomasdao
  • 2,927
  • 1
  • 28
  • 24
  • 4
    This is how the Tab background color is set dynamically. This answer deserves more upvotes... – Gunnar Karlsson Oct 13 '12 at 08:18
  • i recommend you to style the actionbar via xml styleattributes as described in the first answer. this is because then android will do all the anoying/expensive work like loading drawables and colors before the activity is launched – martyglaubitz Oct 25 '12 at 11:30
  • I think total time user has to wait is still the same, not depends on the order of loading drawables. Do in code only useful/more flexible if for example u need to change color/image in runtime – thomasdao Oct 29 '12 at 09:01
  • 23
    None of these answers actually answer the question. OP has asked how to change the underline color of actionbar tabs. The first code example changes the actionbar background color which appears above the tabs. The second and third examples both call the same method which changes the entire tab background color. Neither of these approaches actually change the light blue tab underline. – Jonathan Ellis Jul 14 '13 at 01:24
  • How can I set the highlight color when pressing a button, the icon / logo? Thanks – SteveEdson Sep 17 '13 at 14:41
11

This may give some clues Action Bar Style Gen

Dori
  • 18,283
  • 17
  • 74
  • 116
10

selectableItemBackground is the attribute I think your looking for. I'd recommend you read this article about Customizing the Action Bar as well as look at this question on SO and this one as well.

In code i cant seem to find a way to customize the individual item selected but , customizing the bar itself would look something like this.

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("FF0000"));
Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80
  • I am creating ActionBar in java. ActionBar bar = getActionBar(); So how do I set style on that ? – user533844 Sep 28 '11 at 16:51
  • Thanks for your help but I just need to set diff color on the tabs. – user533844 Sep 28 '11 at 20:38
  • I'm not sure this can be done in code. If your requirement is in code only, you should probably specify that in the question. – Terrance Sep 28 '11 at 21:36
  • This is the way to change the action bar background color. If I need to change the action bar tabs background color programatically means, how can I change?Because in the portrait mode action bar is in split mode, that is action bar title with logo and tabs. – Karthick Apr 22 '13 at 03:46
7
ActionBar bar = getActionBar();

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// set background for action bar
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0c2354")));

// set background for action bar tab
bar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#B5C0D0")));       

bar.show();
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
3

Here is a much easier way:

I've been struggling with this for days, but finally found the solution. I'm using AppCompat. You can set colorAccent in your theme and that will change the highlight color on your ActionBar. Like so:

<item name="colorAccent">@color/highlightcolor</item>

Here it is in context:

<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/darkgrey</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/highlightcolor</item>
</style>

Where I originally posted this answer: Android Tab underline color not changing

Community
  • 1
  • 1
Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
  • I've been searching for this since a few days, but couldn't find any precise solution. Thank you for this answer, it really helped. – Taha Rushain May 04 '15 at 16:39
2

1) Generate the Action Bar Style, click Download ZIP to get the files

2) When you extract the zip file created in Step 1, you will get a res folder. Add this folder to your project under platform/android.

3) Modify manifest.xml to Use the New Action Bar Style, where "Action" is name of your style set in step 1.

<android xmlns:android="http://schemas.android.com/apk/res/android">      
<tool-api-level>14</tool-api-level>
<manifest>
    <application android:theme="@style/Theme.Action"/>
    <uses-sdk android:minSdkVersion="14"
        android:targetSdkVersion="16"/>
</manifest>
</android>

This manual helped me and I hope it will help you too.

Michal
  • 2,071
  • 19
  • 30