3

Is there a way to change the Text color or background color of a single ActionBar menu item by code ?

To be more clear, only one item of the actionBar should have a different color, the rest of them should be the original color.

If so, how exactly can I achieve this. Thank you.

Phantom
  • 968
  • 3
  • 14
  • 32
  • http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2 – Vrashabh Irde Feb 27 '15 at 10:28
  • http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2 – Rohit Goswami Feb 27 '15 at 10:29
  • Thanks for the replies, but those posts don't answer exactly my question, as I need to change only one item of the actionBar, the rest of them remaining the original color. – Phantom Feb 27 '15 at 10:32

3 Answers3

0

add this lines at styles.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#de5188</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="android:subtitleTextStyle">@style/MyActionBarSubTitleText</item>
</style>

<style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="MyActionBarSubTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">10dp</item>
</style>

And change your manifasted as

 android:theme="@style/AppTheme"

I hope this one help to you :)

user3515851
  • 469
  • 1
  • 4
  • 14
0

Background :

 ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); //<-- Your Color

TextColor :

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

I think you can use both answer @user3515851 and mine.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

It would be better if you used a custom Layout for action bar for some cases like this.

actionBar.showCustomView(true)// or similar to this.
actionBar.setCustomView(view)// you can inflate the view as you wish

so you can customise as it was a normal layout like RelativeLayout.

I hope this helps.

Ercan
  • 3,705
  • 1
  • 22
  • 37