Drag TextView to TextView

I am trying to create a drag and drop application, but I cannot drag text from textview to textview, but from textview, to edit its text with this code.

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements OnLongClickListener {

private TextView textView;
private TextView textView1;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    myDragEventListener mDragListen = new myDragEventListener();

    textView.setTag("Example");

    textView.setOnLongClickListener(this);

    textView1 = (TextView)findViewById(R.id.textView1);

    textView1.setOnDragListener(mDragListen);
}


protected class myDragEventListener implements View.OnDragListener
{

    @Override
    public boolean onDrag(View v, DragEvent event) 
    {
        final int action = event.getAction();

        switch(action) 
        {

        case DragEvent.ACTION_DRAG_STARTED:

            if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) 
            {

                return true;
            }

            // Returns false. During the current drag and drop operation, this View will
            // not receive events again until ACTION_DRAG_ENDED is sent.
            return false;

        case DragEvent.ACTION_DRAG_ENTERED:


            return true;

        case DragEvent.ACTION_DRAG_LOCATION:

            // Ignore the event
            return true;

        case DragEvent.ACTION_DRAG_EXITED:


            return true;

        case DragEvent.ACTION_DROP:

            if(v == findViewById(R.id.textView1)) 
            {
                TextView view = (TextView) event.getLocalState();
                textView1.setText(view.getText().toString());
            } 
            else
            {
                View view = (View) event.getLocalState();
                view.setVisibility(View.VISIBLE);
                Toast.makeText(MainActivity.this, "You can't drop the image here", Toast.LENGTH_LONG).show();
                break;
            }
            break;

        case DragEvent.ACTION_DRAG_ENDED:

            v.invalidate();

            // Does a getResult(), and displays what happened.
            if (event.getResult())
            {
                Toast.makeText(MainActivity.this, "The drop was handled.", Toast.LENGTH_LONG).show();
            }
            else 
            {

            }

            // returns true; the value is ignored.
            return true;

            // An unknown action type was received.
        default:
            Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
            break;
        }

        return false;
    }
}


@Override
public boolean onLongClick(View view)
{
    TextView textView = (TextView) view;
    ClipData.Item item = new ClipData.Item(textView.getText().toString());
    String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
    ClipData data = new ClipData("", mimeTypes, item);
    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

    view.startDrag( data,
            shadowBuilder,//data to be dragged
            view, //local data about the drag and drop operation
            0   //no needed flags
            );

    return true;
};
}

      

Where is my mistake?

+3


source to share


1 answer


longClickListener()

Use instead onTouchListener()

. Let me know if it doesn't work.



textView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
                // TODO Auto-generated method stub
                ClipData data = ClipData.newPlainText("datalabel", "text");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
                v.startDrag(data, shadow, null, 0);
                return false;
            }
        });

      

0


source







All Articles