A Spinner shows a drop-down menu using a ListPopupWindow , you can use it to display a selection list of select items:
private void showPopup() { final ListPopupWindow lpw = new ListPopupWindow(this); lpw.setAdapter(); lpw.setAnchorView(mAnchor);
You can then call this method from the onOptionsItemSelected() callback when the correct MenuItem selected. You need to take care of two things:
mAnchor is a View that you need to insert into the Activity layout in the upper right corner so that ListPopupWindow displayed in the correct position. For example, if you have an Activity root:
a RelativeLayout , then mAnchor will be:
mAnchor = new View(this); RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(0, 0); rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); mAnchor.setLayoutParams(rlp);
a LinearLayout , then mAnchor will be:
mAnchor = new View(this); LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(0, 0); llp.gravity = Gravity.RIGHT; mAnchor.setLayoutParams(llp);
etc. for other types of layouts.
Secondly, you need to adjust the width of the ListPopupWindow to the desired value. You will need to adapt this value for different screen sizes and orientations (for example, a portrait of a phone and a landscape phone, different table sizes in portrait and landscape orientation).
Luksprog
source share