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;
}