6

I have a popup menu implemented , which shows up on click of a button. This is my onclick method.

public void showOverflow(View view) {

    boolean click = true;
    Button action = (Button) findViewById(R.id.btbAction);

    LayoutInflater inflater = (LayoutInflater) main.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.overflow_layout, null);
    final PopupWindow pw = new PopupWindow(popupView,
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(true);
    if (click) {
        pw.showAsDropDown(action, 0, 0);
        click = false;
    } else {
        pw.dismiss();
        click = true;
    }
}

The popup window shows up when the button is clicked. Now, the problem is that the window is not dismissed when i touch outside the popup window. I tried setting this property to the popup window

pw.setOutsideTouchable(true);

Things remain the same. Please help me fix this

darsh
  • 741
  • 4
  • 10
  • 34
  • Hi please check below http://stackoverflow.com/questions/8823606/how-to-consume-and-dismiss-the-popupwindow-when-clicked-outside – Nikhil May 14 '12 at 04:52

3 Answers3

5

You should change the setOutsideTouchable call's parameter to true: pw.setOutsideTouchable(false);

Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the update() methods.

Parameters: touchable true if the popup should receive outside touch events, false otherwise

On the other hand, what is the click local variable supposed to do? It is set to true, so it will always force the pw to pop up, whenever the showOverflow method is called, and for no reason it is set to false later, because it's life cycle ends as you leave that method.

Your code should look something like this:

private LayoutInflater inflater;
private Button action;
private PopupWindow pw;
private View popupView;
/*
 * (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    popupView = inflater.inflate(R.layout.overflow_layout, null, false);

    action = (Button) findViewById(R.id.action);
    action.setOnClickListener(this);
}

public void showOverflow()
{
    pw = new PopupWindow(getApplicationContext());
    pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(true);

    pw.setContentView(popupView);
    pw.showAsDropDown(action, 0, 0);
}

The getApplicationContext() shoud be used in case you are inside an Activity class. Otherwise you should get the Context as a parameter.

Zaffy
  • 16,801
  • 8
  • 50
  • 77
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • all I want is to dismiss the popup window (in poped up state) when i click outside the window, in the underlying activity. The local variable click is set to true when the popup is active. so when another click is performed, I check whether boolean click is false, so that i can dismiss the popup. Don't know whether I am doing it correctly. – darsh May 14 '12 at 05:24
  • tried pw.setOutsideTouchable(false); the popup is not dismissed when I touch outside – darsh May 14 '12 at 05:25
  • 1
    please see my update for using the context in creating the `PopupWindow`. The `setOutsideTouchable` method can be modified, if you need your popup to be focusable / touchable. In this case you'll need a touch interceptor, to dismiss the popup when a `MotionEvent.ACTION_OUTSIDE` occurs. – rekaszeru May 14 '12 at 06:18
  • and what does the LogCat say? Could you please paste the error's stack trace too? – rekaszeru May 14 '12 at 06:50
  • Could you please paste the whole relevant output by editing your question? these lines do not help tracking the source of the exception. – rekaszeru May 14 '12 at 09:57
  • I've edited the question. Please have a look at the error log – darsh May 14 '12 at 10:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11224/discussion-between-rekaszeru-and-stephensherbatsky) – rekaszeru May 14 '12 at 10:28
0

change pw.setOutsideTouchable(true); to pw.setOutsideTouchable(false);

Carl
  • 251
  • 1
  • 4
  • 13
0

I know this is an old question but this is what I have done to fix it

The problem is:

You are creating a new instance of popupwindow everytime you call showOverFlow() thats why after you close the popupwindow another popup window will show

What will you do is initialize popupview in OnCreate

Then call popupwindow.showAsDropDown(view) in showOverFlow() method

And lastly you can check whether is it showing below code

Put this code in your button onclick

  if(popupwindow.isShowing()){
  popup.dismiss() }
  else{
  ShowOverflow()}
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21