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 ar
I encountered the same behaviour. I have found the reason, which is located in TextView class.
The method onDragEvent(DragEvent event) is overrriden here and looks as below.
@Override
public boolean onDragEvent(DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return mEditor != null && mEditor.hasInsertionController();
case DragEvent.ACTION_DRAG_ENTERED:
TextView.this.requestFocus();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
final int offset = getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection((Spannable)mText, offset);
return true;
case DragEvent.ACTION_DROP:
if (mEditor != null) mEditor.onDrop(event);
return true;
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_EXITED:
default:
return true;
}
}
If it is possible to insert text => EditText then any drag will be processed and accepted there. The View class is not using OnDragListener, so it is not possible to prevent this behaviour by
editText.setOnDragListener(null);
The solution here is to subclass the EditText as here and override onDragEvent() method:
public class YourEditText extends EditText {
...
// other stuff
...
@Override
public boolean onDragEvent(DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
}
return false;
default:
return super.onDragEvent(event);
}
}
}
Now YourEditText will accept only drags with MIMETYPE_TEXT_PLAIN. If you want to disable the drag drop at all just return false in this method`
@Override
public boolean onDragEvent(DragEvent event) {
return false;
}
And that's it. Hope it will help.