Android - performs drag and drop programmatically

I would like to programmatically drag one view and release it on top of another for initialization purposes, is this possible?

EDIT: I am looking for a way to simulate drag & drop

+4
source share
1 answer

You can remove views from layouts and add them to other layouts (if that's what you are looking for)

ViewGroup oldGroup = (ViewGroup) findViewById(R.id.some_layout); ViewGroup newGroup = (ViewGroup) findViewById(R.id.some_other_layout); Button button = (Button) oldGroup.findViewById(R.id.a_button); oldGroup.removeView(button); newGroup.addView(button); 

There are no drag and drop animations, and this may give strange results, but it is possible.

A ViewGroup will be LinearLayout , RelativeLayout and those.


To model Drag & Drop events, you can call onDragListener manually, but there is one problem:

 public boolean onDrag(View v, DragEvent event); 

expects a DragEvent that has no public constructor, so you can only call it null

There may be a way to create it through a fake batch, but it will be ugly.

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

Another possibility is to maybe create touches, but Idk if that works. Perhaps it is possible monkey .

+3
source

Source: https://habr.com/ru/post/1402129/


All Articles