Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startDrag method Deprecated and unable to compile the program

startDrag(android.content.ClipData, android.view.View.DragShadowBuilder, java.lang.Object, int) is deprecated. How to solve this without losing compatibility to the older versions? Are there any alternatives? I'm learning android basics and while trying out a simple drag and drop exercise, I encountered this error.

like image 837
Dante Avatar asked Sep 20 '16 14:09

Dante


2 Answers

According to Androids API reference startDrag() was deprecated in API level 24

Use startDragAndDrop() for newer platform versions.

And since Android API level 24 equals Android N you can use:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    view.startDragAndDrop(...);
} else {
    view.startDrag(...);
}
like image 114
kirodge Avatar answered Nov 11 '22 10:11

kirodge


startDrag was very recently deprecated - in API 24. So you could use startDragAndDrop instead and differentiate between versions.

What you could also say is preserving compatibility to lower versions. The thing is Drag&Drop was introduced in API 11. So you could try differentiating between versions:

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    //MyDragEventListener comes in here
}

Otherwise as said, there is no official Drag&Drop for < API 11. There is not much use of implementing it for Android below API 14 (or API 15 for that matter), because per Android Studio, there are only a handful of devices running below that version, i.e. only mere 2.3%.

If you still, insist on doing it, you could use a third party library such as Android-DragArea.

Hope this helps!

like image 32
Dimitar Avatar answered Nov 11 '22 09:11

Dimitar