Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling action item background ActionBarSherlock

I've been trying to create my own action item, but it has benn impossible for me.

What I'm tryind to do, is creating a custom action item layout (color background for example), something like "Thumbs" action bar. I only want to change action items only for one of my activities.

enter image description here

I've been playing with android:icon and android:actionLayout property for action menu items.. but I got nothing.

Actually, I've seen another threads in StackOverflow, but it didn't help me ...

Building ActionMode with custom layout in ActionBarSherlock

styling ActionbarSherlock: textColor of the action-items

Any ideas? Thanks in advance!

like image 856
cesards Avatar asked Dec 15 '22 22:12

cesards


1 Answers

To override ActionBarSherlock themes you should proceed like this:

Open values/abs__themes.xml from ActionBarSherlock library project. You see for example:

<style name="Theme.Sherlock" parent="Sherlock.__Theme">
    <!-- Action bar styles (from Theme.Holo) -->
    <item name="actionDropDownStyle">@style/Widget.Sherlock.Spinner.DropDown.ActionBar</item>
    <item name="actionButtonStyle">@style/Widget.Sherlock.ActionButton</item>
    <item name="actionOverflowButtonStyle">@style/Widget.Sherlock.ActionButton.Overflow</item>
    <item name="actionModeBackground">@drawable/abs__cab_background_top_holo_dark</item>
    <item name="actionModeSplitBackground">@drawable/abs__cab_background_bottom_holo_dark</item>
    <item name="actionModeCloseDrawable">@drawable/abs__ic_cab_done_holo_dark</item>
    <item name="actionBarTabStyle">@style/Widget.Sherlock.ActionBar.TabView</item>
    ...
    // Here is what you wanted
    <item name="actionBarItemBackground">@drawable/abs__item_background_holo_dark</item>
    ...

When you have found the item you want to customize (actionBarItemBackground in your case), you create your own themes.xml inside your project, and add in it:

<style name="Custom.Theme.Sherlock" parent="@style/Theme.Sherlock">
    <item name="actionBarItemBackground">@drawable/my__item_background_holo_dark</item>
</style>

This overrides the default Theme.Sherlock, setting a custom actionBarItemBackground.

Now, instead of using Theme.Sherlock in your activity you should use setTheme(R.style.Custom_Theme_Sherlock). You may also want to override the other two themes (Theme.Sherlock.Light and Theme.Sherlock.Light.DarkActionBar)

One more tip, here is the drawable selector used by ActionBarSherlock for the default action item background (in holo_light), it uses 9-patch png drawables:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_light" />
<item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_light" />
<item android:state_focused="true"                                                             android:drawable="@drawable/abs__list_focused_holo" />
<item                                                                                          android:drawable="@android:color/transparent" />


For other basic customizing, you can use this tool, it generates styles for you.

like image 94
Michel-F. Portzert Avatar answered Jan 09 '23 20:01

Michel-F. Portzert