public static void dragVirtualButton(VirtualButton v, MotionEvent event) { float x, y; int action = event.getActionMasked(); switch (action) { case (MotionEvent.ACTION_DOWN): case (MotionEvent.ACTION_MOVE): // Calculation of the new view position x = (v.getLeft() + event.getX() - v.getWidth() / 2) / v.getResources().getDisplayMetrics().widthPixels; y = (v.getTop() + event.getY() - v.getHeight() / 2) / v.getResources().getDisplayMetrics().heightPixels; Helper.setLayoutPosition((Activity) v.getContext(), v, x, y); v.setPosX(x); v.setPosY(y); return; default: return; } }