Android: How to drag(move) PopupWindow?
Solution 1
In eclipse, when looking at the info on getX(), it says: getX(int) for the first pointer index . I am assuming that this should be the only used when setting the first x and y for ACTION.DOWN. When using ACTION.MOVE, getRawX() and getRawY(), eclipse says: original location of the event on the screen. Using it this way, popup window will not flicker and will remain at its location after moving until it's dismissed.
Updated code:
case MotionEvent.ACTION_DOWN:
dx = (int) event.getX();
dy = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
xp = (int) event.getRawX();
yp = (int) event.getRawY();
sides = (xp - dx);
topBot = (yp - dy);
popup.update(sides, topBot, -1, -1, true);
break;
Solution 2
Try this:
mView = mLayoutInflater.inflate(R.layout.popup,
null);
mPopupWindow = new PopupWindow(mView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);
mPopupWindow.showAtLocation(parentView, Gravity.CENTER, mPosX, mPosY);
mView.setOnTouchListener(new OnTouchListener() {
private int dx = 0;
private int dy = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
dx = mPosX - motionEvent.getRawX();
dy = mPosY - motionEvent.getRawY();
break;
case MotionEvent.ACTION_MOVE:
mPosX = (int) (motionEvent.getRawX() + dx);
mPosY = (int) (motionEvent.getRawY() + dy);
pop.update(mPosX, mPosY, -1, -1);
break;
}
return true;
}
});
Solution 3
Try getRawX()
and getRawY()
instead of getX()
and getX()
.
It still bounces a little bit when you touch it again, but it's much smoother when moving.
![jclova](https://i.stack.imgur.com/FmQEW.jpg?s=256&g=1)
jclova
Senior Android developer at DEEP Inc. and CEO at EllevSoft. DEEP Inc. http://deep-inc.com/ EllevSoft https://play.google.com/store/apps/developer?id=EllevSoft
Updated on June 27, 2022Comments
-
jclova almost 2 years
I want to be able to move PopupWindow on touch dragging. I don't want UI to update on the release of the touch. I want PopupWindow to follow my touch.
This is something what I am doing:
mView = mLayoutInflater.inflate(R.layout.popup, null); mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false); mPopupWindow.showAtLocation(parentView, Gravity.CENTER, -5, 30); mView.setOnTouchListener(new OnTouchListener() { private int dx = 0; private int dy = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: dx = (int) motionEvent.getX(); dy = (int) motionEvent.getY(); break; case MotionEvent.ACTION_MOVE: int x = (int) motionEvent.getX(); int y = (int) motionEvent.getY(); int left = (x - dx); int top = (y - dy); Log.d("test", "x: " + left + " y: " + top); mPopupWindow.update(left, top, -1, -1); break; } return true; } });
What happens is, as I drag the popup window, it flickers back and forth on the original location and where my finger is.
Flickering Logcat Result:
x: -44 y: 4 x: -43 y: 37 x: -46 y: 4 x: -46 y: 38 x: -48 y: 4 x: -47 y: 38 x: -50 y: 4
But if I remove (comment out) "mPopupWindow.update(left, top, -1, -1);", it returns correct result. (But obviously UI won't update):
x: -33 y: 0 x: -37 y: 0 x: -41 y: 0 x: -43 y: 3 x: -46 y: 3 x: -50 y: 3 x: -54 y: 4 x: -57 y: 4
How would I move a PopupWindow correctly?