1

I am trying to create a popup window when a certain menu item is pressed. I think I have most of the code, however I am not sure what to do for showAtLocation(...) or showAsDropDown(...).

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
         ...
        case R.id.about:
          displayPopupWindow();
          return true;
         ...
      }
}

public void displayPopupWindow() {
    PopupWindow popup = new PopupWindow(this);
    View layout = getLayoutInflater().inflate(R.layout.popup, null);
    popup.setContentView(layout);
    popup.setOutsideTouchable(true);
    popup.setFocusable(true);
    popup.showAtLocation(??, Gravity.CENTER, 0, 0);
}

What should i put for the view for the menu or should I do this another way? I hope that makes sense and thanks for the help!

frogatto
  • 28,539
  • 11
  • 83
  • 129
soccercam
  • 25
  • 1
  • 5
  • This post might shed some light on the showAtLocation portion: http://stackoverflow.com/questions/7926689/androidpopupwindow-showatlocation – Gravitoid Mar 27 '14 at 14:18

1 Answers1

8

i know its been 4months, maybe you've passed it, but im your solution, and i just signed up here yesterday, so yh.. Here's the solution to your problem, copy and paste..

public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
     ...
    case R.id.about:
      displayPopupWindow();
      return true;
     ...
     }
}

public void displayPopupWindow() {
    PopupWindow popup = new PopupWindow(this);
    View layout = getLayoutInflater().inflate(R.layout.popup, null);
    popup.setContentView(layout);
    popup.setOutsideTouchable(true);
    popup.setFocusable(true);
    popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}

So basically what you do is you use the view you inflated which is "layout" in your case.. Hope it helps, Let me know...

Elltz
  • 10,730
  • 4
  • 31
  • 59