Creating a PopupWindow:
private ArrayAdapter<String> adapterTypeSelection;
private void popupWindow(View v, Point p) {
// int popupwindowWidth =
// UnitConverterClass.convertDpToPx(180,getActivity());
int popupwindowHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(
R.layout.dashboard_profile_popup_window, null);
// Creating the PopupWindow
final PopupWindow pwindow = new PopupWindow(getActivity());
pwindow.setContentView(layout);
// pwindow.showAsDropDown(v);
// pwindow.setWidth(popupwindowWidth);
pwindow.setHeight(popupwindowHeight);
pwindow.setFocusable(true);
String[] types = {"hello", "hi"};
adapterTypeSelection = new ArrayAdapter<String>(getActivity(),
R.layout.<your layout for the popupwindow>,
R.id.textView, types);
ListView listview = (ListView) pwindow.getContentView().findViewById(
R.id.listview_popwindow);
listview.setAdapter(adapterTypeSelection);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView temp = (TextView) parent.getChildAt(position)
.findViewById(R.id.textView);
if (temp.getText()
.toString()
.equals("hello"))) {
//hello
} else {
//hi
}
pwindow.dismiss();
}
});
pwindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
//TODO dismiss settings
}
});
pwindow.setWidth(<width>);
pwindow.setBackgroundDrawable(<resource for background>);
// int OFFSET_X = UnitConverterClass.convertDpToPx(180, getActivity());
// int OFFSET_Y = UnitConverterClass.convertDpToPx(30, getActivity());
// pwindow.showAtLocation(layout, Gravity.NO_GRAVITY,
// p.x + OFFSET_X, p.y + OFFSET_Y);
pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);
}
Then you create a OnClickListener for the button like this:
actionBarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
Point point = new Point();
point.x = location[0];
point.y = location[1];
popupWindow(v, point);
}
}
);
Hope this helps if you really want to use a PopupWindow but I would still go for the custom layout on the menu.