Search code examples
javaandroidillegalstateexceptionandroid-9.0-pie

IllegalStateException: drag shadow dimensions must be positive


https://developer.android.com/reference/android/view/View.DragShadowBuilder#public-constructors

When I use the second constructor outlined above - "View.DragShadowBuilder() Construct a shadow builder object with no associated View."

the following exception is raised: java.lang.IllegalStateException: Drag shadow dimensions must be positive

The second line of code here causes this for me:

public boolean onLongClick(View view) {
    view.startDragAndDrop(null, new View.DragShadowBuilder(), view, 0); 
    return true;
}

It occurs on Android 9 Pie only. I have tried it on previous Android versions with no problem. I created an issue on the Android issue tracker, but maybe someone here knows why it happens or how to resolve it.

In the meantime I am creating the new View.DragShadowBuilderView(view) with view being a transparent ImageView which stops the crash for now but I am not sure if it is the best solution.


Solution

  • There is no need to create a transparent view. Create a custom DragShadowBuilder

    public class MyDragShadowBuilder extends View.DragShadowBuilder {
    
        @Override
        public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) {
            outShadowSize.set(1,1);
            outShadowTouchPoint.set(0,0);
        }
    }
    

    and replace new View.DragShadowBuilder() with new MyDragShadowBuilder().