1

I am creating an android application that will drop image on the main layout. My problem is how to drag the image and drop it on another layout.

Some of my code came from: http://www.vogella.com/articles/AndroidDragAndDrop/article.html

My code is working as long as you drag it on the same layout it doesnt move on the other layout.

This is my code:

public class MainActivity extends Activity {    
int _xDelta;
int _yDelta;
  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

}

 private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {


       final int X = (int) motionEvent.getRawX();
        final int Y = (int) motionEvent.getRawY();
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
        }
        return true;
}
 }

  class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
  int action = event.getAction();
  switch (event.getAction()) {
  case DragEvent.ACTION_DRAG_STARTED:
     break;
  case DragEvent.ACTION_DRAG_ENTERED:
      break;
  case DragEvent.ACTION_DRAG_EXITED:
    break;
  case DragEvent.ACTION_DROP:
    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:
  default:
    break;
  }
  return true;
}

} }

Hope you can help me.

MMakati
  • 693
  • 1
  • 15
  • 33

1 Answers1

0

I made a touched view invisible, created the same-looking view programmatically at the place of an old view and set this new view for moving (root is the root layout for newly created view).

     ImageView tmpView;
     private float dX;
     private float dY;
     public boolean onTouch(View view, MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:

                    view.setVisibility(View.GONE);
                    Log.d(TAG, "event.getRawX() " + event.getRawX() + ", event.getRawY() " + event.getRawY());
                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();

                    tmpView = new ImageView(SongActivity.this);
                    tmpView.setImageDrawable(((ImageView) view).getDrawable());

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
                    params.leftMargin = (int) event.getRawX();
                    params.topMargin = (int) event.getRawY();
                    root.addView(tmpView, params);

                    RelativeLayout.LayoutParams ivLayoutParams = (RelativeLayout.LayoutParams) tmpView.getLayoutParams();
                    ivLayoutParams.leftMargin = (int) (event.getRawX());
                    ivLayoutParams.topMargin = (int) (event.getRawY() - 250);
                    ivLayoutParams.rightMargin = -250;
                    ivLayoutParams.bottomMargin = -250;
                    tmpView.setLayoutParams(ivLayoutParams);

                    break;
                case MotionEvent.ACTION_MOVE:
                    tmpView.setY(event.getRawY() - 250);
                    tmpView.setX(event.getRawX() + dX);
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    return false;
            }
            return true;
        }
Max Makeichik
  • 227
  • 4
  • 18