Java – Android – Programmatically perform drag-and-drop

Android – Programmatically perform drag-and-drop… here is a solution to the problem.

Android – Programmatically perform drag-and-drop

I want to programmatically drag one View and release it on top of another for initialization, is this possible?

EDIT: I’m looking for a way to simulate drag and drop


You can remove Views from a layout and add them to other layouts if that’s what you’re looking for

ViewGroup oldGroup = (ViewGroup) findViewById(;
ViewGroup newGroup = (ViewGroup) findViewById(;

Button button = (Button) oldGroup.findViewById(;


Without drag-and-drop animations, it can produce strange results, but it is possible.

ViewGroup can be LinearLayout, RelativeLayout, etc.

To simulate drag-and-drop events, you can manually invoke onDragListener, but there is a problem:

public boolean onDrag(View v, DragEvent event);

You need a DragEvent without a public constructor, so you can only call it with null

There may be a way to create one with a fake parcel, but that would be ugly.

private void initializationTest() {
    DragEvent event = null;
    /* maybe sth like that
    Parcel source = Parcel.obtain();
    event  = DragEvent.CREATOR.createFromParcel(source);
    onDrag(theTargetView, event);

Another possibility is to create touchevents, but don’t know if that works. At least monkey might do that.

Related Problems and Solutions