title: Popup Window Android
body: <p>I have a button: "btnOpenPopup" (in a scrollview), when i click the button, a popup window will appear, I can move this popup window on the screen</p>
  
  <p>Here is my code:</p>
  
  <pre><code>final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.buttonUndoRedo);
      btnOpenPopup.setOnClickListener(new ImageButton.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
  
              if (popupWindowEnabled == true) {
                  popupWindowEnabled = false;
                  LayoutInflater layoutInflater =
                          (LayoutInflater) getBaseContext()
                                  .getSystemService(LAYOUT_INFLATER_SERVICE);
                  final View popupView = layoutInflater.inflate(R.layout.popup_window, null);
                  final PopupWindow popupWindow = new PopupWindow(
                          popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  
                  // Font
                  TextView txt = (TextView) popupView.findViewById(R.id.textViewPopupWindow);
                  Typeface myNewFace = Typeface.createFromAsset(getAssets(), "fonts/futura.ttf");
                  txt.setTypeface(myNewFace);
  
                  Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
  
                  btnDismiss.setOnClickListener(new Button.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          popupWindow.dismiss();
                          popupWindowEnabled = true;
                      }
                  });
  
                  popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
  
                  popupView.setOnTouchListener(new View.OnTouchListener() {
                      int orgX, orgY;
                      int offsetX, offsetY;
  
                      @Override
                      public boolean onTouch(View v, MotionEvent event) {
                          //Toast.makeText(getApplicationContext(), "dEMO", Toast.LENGTH_SHORT).show();
                          switch (event.getAction()) {
                              case MotionEvent.ACTION_DOWN:
                                  orgX = (int) event.getX();
                                  orgY = (int) event.getY();
                                  break;
                              case MotionEvent.ACTION_MOVE:
                                  offsetX = (int) event.getRawX() - orgX;
                                  offsetY = (int) event.getRawY() - orgY;
                                  popupWindow.update(offsetX, offsetY, -1, -1, true);
                                  break;
                          }
                          return true;
                      }
                  });
              }
  
          }
  
      });
  </code></pre>
  
  <p>The problem is when I move btnOpenPopup in scrollview, the popupwindow will leave its position and follow the btnOpenPopup</p>
  
  <p>How to prevent popup window from moving when we move btnOpenPopup? 
  Thanks in advance!</p>