Android Drag and Drop TextView goes missing if not dropped properly

I have textview that appears above empty linear layout. Everytime I have to drag and drop the textview into the linearlayout or else it goes missing. I want to be able to counter in case the user didn't complete the drop properly the textview still exist there. Below is my code for TouchListener:

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

This is my code for DragListener

class MyDragListener implements View.OnDragListener {
    Drawable enterShape = getResources().getDrawable(
            R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundDrawable(enterShape);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                View view = (View) event.getLocalState();
                ViewGroup owner = (ViewGroup) view.getParent();
                owner.removeView(view);
                LinearLayout container = (LinearLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(normalShape);
            default:
                break;
        }
        return true;
    }
}

Upvotes: 1

Views: 473

Answers (1)

case DragEvent.ACTION_DRAG_ENDED:
                View view = (View) event.getLocalState();
                view.setVisibility(View.VISIBLE);
                v.setBackgroundDrawable(normalShape);
                break;

I edited the case for DragEvent.ACTION_DRAG_ENDED and got the solution I was looking for.

Upvotes: 1

Related Questions