Question

I'm doing a little drag n drop an image into layouts. Everything is working fine, except sometimes the image are disappearing when you touch it.

Any idea why? I don't seem to understand why is sometimes disappearing, I think I'm catering for every possibility. Your help will be greatly appreciated. Thanks!

This is my code so far...

public class MainActivity extends Activity {

private static final String LOGCAT = null;

/*
 * Here are the links I've been using
 * http://www.techrepublic.com/blog/software-engineer/try-androids-useful-drag-and-drop-api/
 * http://codingjunkie.net/android-drag-and-drop-part-2/
 * http://javapapers.com/android/android-drag-and-drop/
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.blue_ball).setOnTouchListener(myOnTouchListener);    // My image
    findViewById(R.id.top_container).setOnDragListener(myDragListener);    // My 1st linear layout 
    findViewById(R.id.bottom_container).setOnDragListener(myDragListener); // My 2nd linear layout with "grayed a" image 
}

OnTouchListener myOnTouchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
};

OnDragListener myDragListener = new OnDragListener() {

    @Override
    public boolean onDrag(View layoutview, DragEvent dragevent) {

        int action = dragevent.getAction();
        View view = (View) dragevent.getLocalState();

        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            break;
        case DragEvent.ACTION_DROP:
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) layoutview;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            if (container.getId() == R.id.bottom_container) {
                view.setOnTouchListener(null);
                view.setOnDragListener(null);
            }
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(LOGCAT, "Drag ended");
            if (dropEventNotHandled(dragevent)) {
                view.setVisibility(View.VISIBLE);
            }
            break;
        default:
            break;
        }
        return true;
    }

    private boolean dropEventNotHandled(DragEvent dragEvent) {
        return !dragEvent.getResult();
    }
};

}

This is my Error message:

02-05 14:48:03.523: E/WindowManager(410): Unable to transfer touch focus
02-05 14:48:03.523: W/WindowManager(410): Drag is in progress but there is no drag window handle.
Was it helpful?

Solution

Thanks JRowan, I tried that and didn't work, but helped me realize where the problem was. After trying so many different things, I figured that I need to add this to the OnTouchListener:

if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        view.setVisibility(View.VISIBLE);

Very simple, no more disappearing buttons. Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top