2

I need a custom drop-down menu attached to an action item. I managed to do the same thing for the home icon, but it seems that it's done totally different with other action items.

I know this question and answer: How to add a Dropdown item on the action bar My problem is that I want to fully customize it (I use custom fonts, will also need icons), so a submenu wouldn't suffice, I suppose. And attaching a custom Spinner as an actionLayout looks nothing like drop down.

For the home icon it looks like this:

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ArrayList<NavItem> data = new ArrayList<NavItem>();
    data.add(...);
    data.add(...);
    ActionBarNavAdapter adapter = new ActionBarNavAdapter(...);
    actionBar.setListNavigationCallbacks(adapter, new OnNavigationListener() {...});

And I could add my own design to it without sacrificing the "drop-down" thing. How to do it with other action items?

EDIT: I know that activity using NAVIGATION_MODE_LIST uses Spinner (ListNavigationCallbacks uses a SpinnerAdapter) but it looks better and I don't know where to get all those styles (if it's just that) to make it look like that. I mean:

Good -> How NAVIGATION_MODE_LIST looks: it's wrapping the list, it's attached under the home icon. Bad -> How a custom Spinner as an actionLayout looks: it matches the screen width with some additional dark background, replaces the action item icon (this is easy to change) and above all, darkens the rest of the screen. It look rather like an AlertDialog with a custom list, not a drop-down menu.

Code:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem sortItem = menu.findItem(R.id.sort);
    Spinner spinner = new Spinner(this);

    //this is the same adapter because I didn't want to spend time creating a new one
    //ActionBarNavAdapter extends SpinnerAdapter of course
    ArrayList<NavItem> data = new ArrayList<NavItem>();
    data.add(...);
    data.add(...);
    ActionBarNavAdapter adapter = new ActionBarNavAdapter(...);
    spinner.setAdapter(adapter);
    sortItem.setActionView(spinner);
    return true;
}
Community
  • 1
  • 1
Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • Considering that what you consider a "drop down" *is* a `Spinner`, a `Spinner` looks precisely like a `Spinner`. You can tell this by looking at an activity using `NAVIGATION_MODE_LIST` in Hierarchy View. Perhaps you might consider editing your question to supply the source code that was giving you the results that you did not like. – CommonsWare Aug 31 '12 at 22:55
  • Thanks for commenting. I updated my question with the code and also explained what I don't like about the Spinner. – Michał Klimczak Aug 31 '12 at 23:11

1 Answers1

3

Try replacing:

Spinner spinner = new Spinner(this);

with:

Spinner spinner = new Spinner(getActionBar().getThemedContext());

This will use a Context themed to match the action bar, and should help.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Nothing changed unfortunately. I used `getSupportActionBar()` as it's ActionBarSherlock lib. – Michał Klimczak Aug 31 '12 at 23:16
  • @MichałK: For ActionBarSherlock, AFAIK, what Jake is using is `com.actionbarsherlock.internal.widget.IcsSpinner` or something else from the `com.actionbarsherlock.internal` set of packages. That is not part of the ActionBarSherlock public API. You would need to clone that code and any necessary resources (in case he changes his implementation in ways that are unsuitable for you) and arrange to use that where needed. "Where needed" would definitely include API Level 10 and below, and it might include 11-13 as well. If you try running my suggestion on ICS/JB, I think it may work as advertised. – CommonsWare Aug 31 '12 at 23:31
  • Duh, looks like a lot of work to do... Maybe it would be easier with custom ActionProvider? Something like ShareActionProvider. I just don't know where to start (and is it a good idea anyway) – Michał Klimczak Aug 31 '12 at 23:40
  • @MichałK: ActionBarSherlock has a backport of `ShareActionProvider`. While `ShareActionProvider` is pretty tied to the notion of listing a bunch of activities based upon an `Intent`, you could clone his implementation and populate yours some other way. A quick glance [at his code](https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/widget/ShareActionProvider.java) suggests that everything you need is in the public API (no `internal` references). – CommonsWare Aug 31 '12 at 23:45
  • I'm gonna try this approach. Btw, lot of work for something that simple... And thanks for help:) – Michał Klimczak Aug 31 '12 at 23:51