6

I have an ActionBarSherlock on my form. I'm reading style information at runtime. One of the pieces of style is the background color of the ActionBar. How can I change this at runtime? The color can be any RGB value.

Eric C
  • 161
  • 2
  • 9
  • Look to this one http://stackoverflow.com/questions/10064411/change-actionbarsherlock-background-color – sonida Jan 05 '13 at 08:00

3 Answers3

8

Maybe this help : How to set title color in ActionBarSherlock? via style or getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak)); via programmatically

With the theme

// add theme in app
<application android:theme="@style/MainTheme"></application>

// MainTheme
<style name="MainTheme" parent="Theme.Sherlock.Light.DarkActionBar">       
</style>

// MainThemeGreen
<style name="MainThemeGreen" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MainTheme.ActionBarStyle</item>        
</style>

// ActionBar
<style name="MainTheme.ActionBarStyle" parent="Widget.Sherlock.Light.ActionBar">
    <item name="android:background">@drawable/bg_green_actionbar</item>
    <item name="android:titleTextStyle">@style/MainTheme.ActionBar.TitleTextStyle</item>
</style>

// Text style
<style name="MainTheme.ActionBar.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
    <item name="android:textColor">@color/White</item>
</style>

// bg_green_actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#ff74af3b" />
        </shape>
    </item>
</layer-list>

After this you can change Theme on fly: setTheme(R.styles.MainThemeGreen);

Community
  • 1
  • 1
beshkenadze
  • 646
  • 5
  • 12
  • You are still specifying a local drawable resource. I need to do it for any color that presented to me from a web service. – Eric C May 17 '12 at 13:31
  • I see your new edit but I'm not following how this helps. After the app starts, I call a web service and it returns a color. Say it is #123456. How do I apply that to your hard coded #FF74af3b? – Eric C May 21 '12 at 14:33
6

One way:

mSupportActionBar = getSupportActionBar();
mSupportActionBar.setBackgroundDrawable(new ColorDrawable(0xff123456));

where 0xff123456 is your required ARGB integer.

Trevor
  • 10,903
  • 5
  • 61
  • 84
5

I just used below Code

 getSupportActionBar().setBackgroundDrawable(new 
   ColorDrawable(Color.parseColor("#00853c"))); 

it changed the bg color. Hope, it helps.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
sravan
  • 5,303
  • 1
  • 31
  • 33