Search code examples
javaandroidonclicklisteneronlongclicklistener

Why does onClick does not fire drag listener, but on Long click works fine?[android]


I have apple on screen and i have 2 listeners attached to it: onClickListener, onDragListener. Simple click on apple image does not fire drag, but long click works fine.

package nis.drag_apple;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

ImageView square ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView apple = (ImageView)findViewById(R.id.apple);
    square = (ImageView)findViewById(R.id.square);

    apple.setTag("apple");
    square.setTag("square");

    apple.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View clicked_apple) {
            Toast.makeText(getApplicationContext(), " on click apple!", Toast.LENGTH_SHORT).show();
            ClipData.Item item = new ClipData.Item((CharSequence)clicked_apple.getTag());
            String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
            ClipData data = new ClipData(clicked_apple.getTag().toString(), mimeTypes, item);
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(clicked_apple);

            clicked_apple.startDrag(data, //data to be dragged
                    shadowBuilder, //drag shadow
                    clicked_apple, //local data about the drag and drop operation
                    0   //no needed flags
            );
            return true ;
        }
    });


    apple.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction() ) {

                //signal for the start of a drag and drop operation.
                case DragEvent.ACTION_DRAG_STARTED:

                    Toast.makeText(getApplicationContext(), "DRAG STARTED!", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "the tag is " + v.getTag().toString(), Toast.LENGTH_SHORT).show();
                    // do nothing
                    break;

                //the drag point has entered the bounding box of the View
                case DragEvent.ACTION_DRAG_ENTERED:

                    break;


                case DragEvent.ACTION_DRAG_EXITED:

                    break;

                case DragEvent.ACTION_DROP:
                    Toast.makeText(getApplicationContext(), "action DROP!", Toast.LENGTH_SHORT).show();
                    if(v == square) {
                        View image_moved = (View) event.getLocalState();
                        ViewGroup parent = (ViewGroup)     
                        image_moved.getParent();
                        parent.removeView(image_moved);

                      //  image_moved.setVisibility(View.VISIBLE);
                    } else {
                        View view = (View) event.getLocalState();
                        view.setVisibility(View.VISIBLE);
                        Context context = getApplicationContext();

                        break;
                    }
                case DragEvent.ACTION_DRAG_ENDED:

                default:
                    break;
            }
            return true;
        }
    });
   }

}

Solution

  • For a click event to occur, you must lift your finger (ACTION_UP), which wouldn't make sense with starting a drag action. A long click, on the other hand, happens with your finger still on the screen. (A more appropriate name might have been "long press".)

    If you want to start a drag right when your finger first touches the View, you can do so with an OnTouchListener, when the ACTION_DOWN event happens.