Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent drag drop of custom mimetype to EditText

I have a custom mime type which I am intending to use to drag and drop application objects within the app. This seems to be working but I'm finding that the EditText fields are also accepting the drop action. I don't want this to happen.

First, I've defined the custome mime type like this:

public static final String MIME_TYPE_MYNODE = "com.example.mockup/mynode";

Then, in the onTouch handler for the source object I have:

  @Override
  //-----------------------------------------------------------------------------
  public boolean onTouch (View v, MotionEvent e)
  {
    ...
    else if (e.getAction() == MotionEvent.ACTION_MOVE)
    {
      String[] mimeTypes = {MIME_TYPE_MYNODE};
      ClipData data = new ClipData ("Task Tamer Note", mimeTypes, new ClipData.Item ("unused"));
      View.DragShadowBuilder shadow = new View.DragShadowBuilder(this);
      Object localState = v;
      startDrag (data, shadow, localState, 0);
      return false;
    }
  }
  ...
}

When I "drop" on an EditText widget, it inserts "unused" into the text area. How can I prevent this? Thanks.

like image 928
Peri Hartman Avatar asked Aug 20 '12 16:08

Peri Hartman


1 Answers

Returns true if the drag event was handled successfully, or false if the drag event was not handled. Note that false will trigger the View to call its onDragEvent() handler.

This is statement from the docs of onDrag(View v, DragEvent event). So if you return false then the event is handled by onDragEvent() of EditText. Hence the simplest solutions is:

editText.setOnDragListener(new OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            return true;
        }
    });

In case you would like to perform some functions, specify based on the Drag event and they will be executed.

like image 69
Anubhav Avatar answered Oct 30 '22 22:10

Anubhav