I want to create button with drop down menu, like overflow menu button in ActionBar on ICS. I have problem because PopupMenu there isn't in android 2.x. The second way using Spinner but it's not for me, because Spinner always show the first item on my custom background or my selected item. How can I acheive it? In general, I want ContextMenu but little and on position clicked view. Thanks.
It looks like this :
If your purpose is to recreate an action bar, you could take a look at the ActionBarSherlock project, which backports thoses functionalities.
If you want to be able to pop such a menu anywhere in your app, you could read the implementation of the MenuPopupHelper class implementation in the ActionBarSherlock project source code.
I use PopupWindow for that sort of thing. It's more work than a PopupMenu, where you just give it a Menu and respond to events with OnMenuItemClickListener.onMenuItemClick()
. With a PopupWindow, you provide a content View and handle clicks at a somewhat lower level.
Still, it gets the job done without too much trouble. The showAsDropDown()
method allows you to anchor the popup to another View (position it appropriately as a drop-down or pop-up menu), just as you want. It's been around since API level 1 and works great for me on all my test devices.
An alternative to your requirement could be ,that you create a list view,, position it under your button & set it's visibility to invisible by default & when you click on the button you can toggle the listview's visibility ....
for example
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (LISTVIEW.isShown()) {
LISTVIEW.setVisibility(View.INVISIBLE);
} else {
LISTVIEW.setVisibility(View.VISIBLE);
}
let me know if this helps...