1

I have a dropdown navigation list in my ActionBar. I'd like to change the background color of the list item background depending on their position, as well as the currently selected item.

I've created my own class that extends ArrayAdapter and changes the background like so:

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    v.setBackgroundColor(Color.parseColor(VideoGroup.getColorForId(position)));
    return v;
}

@Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    v.setBackgroundColor(Color.parseColor(VideoGroup.getColorForId(position)));
    return v;
}

This however results in the following – notice that the actual list items look pretty good, but the currently selected one does not.

How can I get the background color for Group 1 here to fill up the navigation button's area as a whole?

slhck
  • 36,575
  • 28
  • 148
  • 201

1 Answers1

1

Unfortunately, the Spinner itself is an image, so it will be hard to make it change colors because you'll ideally need an image for each color. You won't be able to make the background on the text expand beyond the triangle, for example. It can definitely be done, but it will probably mean customizing things quite a bit.

An alternative (and maybe simpler) way to apply a color mask to a view is to use a filter:

    spinner.getBackground().setColorFilter(THE_COLOR_GOES_HERE, PorterDuff.Mode.MULTIPLY);
dmon
  • 30,048
  • 8
  • 87
  • 96
  • Hm, I almost figured so. As for your alternative: Where do I get the `spinner` object in this case? I set it up by calling `mSpinnerAdapter = new FooAdapter(…)` and `getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);` – so there's no spinner object I could refer to. – slhck Dec 01 '12 at 20:46
  • Oh it's in the ActionBar? Hmmmm apparently that's not straightforward either: http://stackoverflow.com/questions/13343852/can-i-disable-actionbars-navigation-spinner – dmon Dec 02 '12 at 22:47
  • Yeah, mentioned it in the first sentence :) Oh well, I think I'm gonna leave it at this then. Thanks for your help. – slhck Dec 03 '12 at 08:05