# DragNDrop

## DragNDrop using Elements on screen:

Create a reusable function using TouchAction class `longPress`\` &  `moveTo` method&#x20;

{% code title="helper.java" %}
```java
  public static void dragNDropByLongpress(WebElement element1, WebElement element2) {
        TouchAction dragNDrop = new TouchAction(driver).longPress(element1).moveTo(element2).release();
        dragNDrop.perform();
    }
```
{% endcode %}

To DragNDrop using Elements, import above function from `helper.java` class

```
test.java

$ helper.dragNDropByLongpress(sourceElement, destinationElement);
```

## DragNDrop using XY-Coordinates of screen:

Create a reusable function using XY-Coordinates, TouchAction class `longPress`\` &  `moveTo` method&#x20;

{% code title="helper.java" %}
```java
  public static void dragNDropByCoordinatesTest(AndroidElement element1, AndroidElement element2) {
        Point center1 = element1.getCenter();
        Point center2 = element2.getCenter();

        TouchAction action = new TouchAction(driver).longPress(center1.x, center1.y).moveTo(center2.x, center2.y).release();
        action.perform();
    }
```
{% endcode %}

To DragNDrop using XY-Coordinates, import above function from `helper.java` class

```
test.java

$ helper.dragNDropByLongpress(sourceElement, destinationElement);
```
