darsh
darsh

Reputation: 751

Issue dismissing popup window

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

Upvotes: 6

Views: 9871

Answers (3)

Cyrille Con Morales
Cyrille Con Morales

Reputation: 957

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()}

Upvotes: 0

Carl
Carl

Reputation: 250

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

Upvotes: 0

rekaszeru
rekaszeru

Reputation: 19220

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.

Upvotes: 5

Related Questions