4

Is there a way to provide a different drawable icon if menu items are pushed to the split actionbar? My split action bar color differs to much from the top bar and a single icon style is not as visually please as I would hope.

sgarman
  • 6,152
  • 5
  • 40
  • 44
  • To be clear, you're interested in changing the icon for `MenuItems`, but only if and when they are placed into the split ActionBar? – adneal Mar 19 '13 at 20:26
  • Yes, two sets of drawables, one for top action bar and one for bottom. – sgarman Mar 20 '13 at 18:40

3 Answers3

0

Put the Drawables that you want to use for split in /res/drawable/ and put the others in /res/drawable-w480dp.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
-1

You can use LevelListDrawable for the icons and set the level when the screen configuration changes (which means Activity.onCreate()is called.

  • Create a drawable to be used in the icons with

    <level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:maxLevel="0" android:drawable="@drawable/**your bitmap dark**" />
    <item android:maxLevel="1" android:drawable="@drawable/**your bitmap light**" />
    </level-list>
    
  • Detect, if ActionBar is splitted or not. This part seems to be tricky. I'm using SherlockActionBar and there it works (up to todays Android Version) this way

  • To change one drawable, set on the corresponding ActionBar.Tab the level with getIcon().setLevel(). Note that you do not need to cast to LevelListDrawable. For whatever reason the level concept is implemented directly with the Drawable class.

Community
  • 1
  • 1
jboi
  • 11,324
  • 4
  • 36
  • 43
-1

In the appcompat library the split ActionBar is used in configurations other than large, land or sw480dp. If youre using the native ActionBar i guess you can skip the large configuration.

split_action_bar_is_narrow

Then, to provide alternative drawables you should define appropriate references in these values dirs, like:

In values:

<item name="action_search_drawable" type="drawable" format="reference">@drawable/ic_action_search_narrow</item>

In values-w480dp:

<item name="action_search_drawable" type="drawable" format="reference">@drawable/ic_action_search</item>

Then, in the menu configuration you should use this reference as the icon drawable.

jakk
  • 1,193
  • 2
  • 12
  • 28