This should work before API 4 (but not tested, YMMV). For instance:
If you use ActionBarSherlock, you can use the IcsListPopupWindow
class. Set some properties on it in onCreate. You will also need a subclass of ArrayAdapter.
in onCreate ():
mPopupMenu = new IcsListPopupWindow(getContext()); mAdapter = new PopupMenuAdapter(this, android.R.layout.simple_list_item_1, yourArrayOfPopupMenuItems); mPopupMenu.setAdapter(mAdapter); mPopupMenu.setModal(true); mPopupMenu.setOnItemClickListener(this); mPopupMenu.setOnDismissListener(this);
Inner classes inside your fragment / action:
private class PopupMenuAdapter extends ArrayAdapter<PopupMenuItem> { Context context; int layoutResourceId; PopupMenuItem data[] = null; public PopupMenuAdapter(Context context, int layoutResourceId, PopupMenuItem[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView;
Whenever you need to show it (e.g. in View.OnClickListener
)
mPopupMenu.setContentWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth() / 2); PopupAdapter.notifyDataSetChanged();
In your OnItemClickListener
Be sure to call mPopupMenu.dismiss()
!
Hope this helps! And thanks to Jake Wharton for the ABS!
source share