btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// infalte menu
PopupMenu popup = new PopupMenu(activity, v);
popup.getMenuInflater().inflate(R.menu.clipboard_popup,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.install:
// Or Some other code you want to put
// here.. This is just an example.
Toast.makeText(
activity,
" Install Clicked at position " + " : "
+ position, Toast.LENGTH_LONG)
.show();
break;
case R.id.addtowishlist:
Toast.makeText(
activity,
"Add to Wish List Clicked at position "
+ " : " + position,
Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
}
});
Updated answer with custom layout
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18);
// where u want show on
//view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (LinearLayout) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
mylayout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>