Search code examples
javaandroiddrag-and-dropdraggable

Drag and Drop in Android: How to get the new position of a dropped View


Hi I got an simple drag and drop activity. It looks like this:

public class Test extends Activity {

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

    findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.one).setOnDragListener(new MyDragListener());
    findViewById(R.id.two).setOnDragListener(new MyDragListener());
    findViewById(R.id.three).setOnDragListener(new MyDragListener());
    findViewById(R.id.four).setOnDragListener(new MyDragListener());
}


//TEST DRAG AND DROP

private final class MyTouchListener implements View.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;
        }
    }
}

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;
    }
}

}

Now I want to get to know the new positions of the Views onClick. I need an int for that. It could be like: if picture one is in layout one, the int is: "11", if picture two is in layout 3 the int would be "23". And I need the positions of all the 4 pictures, so I need 4 int.

But I have no clue, how to get the new position. I'm thankful for any help!


Solution

  • When onClick is triggered, you can simply get the child view position by calling int indexOfChild(View):

    @Override
    public void onClick(View v) {
        int position = parentContainer.indexOfChild(v);
    }
    

    Then, by adding a specific tag for each widget's image. You will be able to construct your int as imageTag + position, for example:

    @Override
    public void onClick(View v) {
        String imageTag = v.getTag();
        int position = parentContainer.indexOfChild(v);
    
        int resultValue = Integer.parseInt(imageTag) + position;
    }