Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue dismissing popup window

Tags:

android

popup

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

like image 775
darsh Avatar asked May 14 '12 04:05

darsh


1 Answers

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.

like image 147
rekaszeru Avatar answered Oct 14 '22 08:10

rekaszeru