Reputation: 4945
I need to make a popup window with a few buttons. The buttons need to be clickable, so i set the popupWindow property as focusable. But as soon as i touch outside the popupWindow, the popup is dismissed. The popup is associated with an EditText. My requirement is such that the user must be able to type into the editText even while the popup is visible.
pWindow = new PopupWindow(context);
pWindow.setBackgroundDrawable(new BitmapDrawable());
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = (RelativeLayout) inflater.inflate(R.layout.popup, null);
popupView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
pWindow.setContentView(popupView);
pWindow.setWidth(popupView.getLayoutParams().width);
pWindow.setHeight(popupView.getLayoutParams().height);
pWindow.setFocusable(true);
pWindow.setTouchable(true);
pWindow.showAsDropDown(anchor, 0, 0);
I have tried various combinations, but unable to achieve the desired results.
Upvotes: 1
Views: 2579
Reputation: 69
In order for your window to stop closing when you click outside, you need to delete this line:
pWindow.setBackgroundDrawable(new BitmapDrawable());
Not sure why exactly, but I know that making the background drawable set it to close on a click outside.
Now as for making the window itself, you should have this in order:
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView =(RelativeLayout) inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.<IdOfLayoutInPopupXML>);
pWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
pWindow.setTouchable(true);
pWindow.showAsDropDown(anchor, 0, 0);
I know this is old but this so at least how I would have it as this is how my popups are working right now. (except I dont show as dropdowns).
Upvotes: 1