Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DragEvent.ACTION_DROP never get called

Tags:

android

Im trying to use image drags on my android app. My problem is that DragEvent.ACTION_DROP is never get called when I stop dragging my image.

In my log cat I get this call:

01-30 13:50:25.003: I/ViewRootImpl(2198): Reporting drop result: false

Thanks for helping.

Here is my code:

public class buildImage extends Activity implements OnTouchListener, OnDragListener
{
    private LinearLayout slider;
    private RelativeLayout board;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.build_image);

        //drawing = (SignatureViewModed) findViewById(R.id.myTest);
        slider = (LinearLayout) findViewById(R.id.imageSlider);
        board = (RelativeLayout) findViewById(R.id.borad);

        setImagesSlider();

    }

    public void setImagesSlider()
    {
        ImageView image = new ImageView(this);
        image.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

        Drawable draw = getResources().getDrawable(R.drawable.images);

        image.setImageDrawable(draw);
        image.setOnDragListener(this);

        image.setOnTouchListener(this);

        slider.addView(image);

    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("tests", "test");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            //view.setVisibility(View.INVISIBLE);
            return true;
          } else {
            return false;
          }
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub

        int action = event.getAction();
          switch (event.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:
          {
              Log.i("check6", "check6");
            // do nothing
            return true;
          }
          case DragEvent.ACTION_DRAG_ENTERED:
          {
              Log.i("check4", "check4");
            //v.setBackgroundDrawable(enterShape);
              return true;
          }
          case DragEvent.ACTION_DRAG_EXITED:
          {
           // v.setBackgroundDrawable(normalShape);
              Log.i("check3", "check3");
              return true;
          }
          case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup

              Log.i("check", "check");
              addNewImage(event);

              return true;
          case DragEvent.ACTION_DRAG_ENDED:
          {
              Log.i("check2", "check2");


             // addNewImage(event);

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

    public void addNewImage(DragEvent event)
    {
        ImageView image = new ImageView(this);
        image.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

        Drawable draw = getResources().getDrawable(R.drawable.images);

        image.setImageDrawable(draw);

        board.addView(image);

        float x = event.getX();
        float y = event.getY();

        image.setX(x);
        image.setY(y);

        Log.i("margin", "x " + x + " y " + y);

    }

}

My XML

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:id="@+id/borad"
            android:background="@android:color/darker_gray"
            android:layout_above="@+id/temp"
            android:layout_height="fill_parent" >
        </RelativeLayout>

        <ScrollView
            android:layout_width="fill_parent"
            android:id="@+id/temp"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/imageSlider"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </LinearLayout>
        </ScrollView>

    </RelativeLayout>

enter image description here

like image 918
dasdasd Avatar asked Jan 30 '14 11:01

dasdasd


1 Answers

You have to have something to drop that image on. So far you only have an OnDragListener on the image itself. The spot you want to drop that image does not contain any control with an OnDragListener, so your Listener won't be called.

Controls that listen to OnDragListeners cannot be invisible or gone, so you have to put some kind of grid there where the cells look empty but aren't. For example an ImageView with a transparent image. Or with Alpha set to 0f.

At least set an OnDragListener to your inner LinearLayout, then you will get Drop events.

like image 66
Jürgen 'Kashban' Wahlmann Avatar answered Oct 31 '22 13:10

Jürgen 'Kashban' Wahlmann