title: How to swap two image views using drag and drop in Android?
body: <p>I've been trying to implement a very simple drag and drop in Android, where the dragged item swaps positions with the item it was dropped on.</p>
  
  <p>I've successfully implemented the onLongClick and onDrag listeners. When I drag and drop an item it replaces the item it was dropped on, but I can't figure out how make the replaced item take the place of the dragged item.</p>
  
  <p>I spent a while searching for answers, but most answers just linked complex implementations of drag and drop involving many files with lots of code, and I wasn't able to understand them enough to apply them to my code.</p>
  
  <p>I originally assumed that I wasn't getting a true copy of the ImageView with</p>
  
  <pre><code>ImageView temp = (ImageView) event.getLocalState();
  </code></pre>
  
  <p>so I tried  </p>
  
  <pre><code>ImageView dragged = (ImageView) findViewById(temp.getId());  
  </code></pre>
  
  <p>but that didn't make a difference.</p>
  
  <p>I'm almost certain the problem is in the 'OnDragListener', in the 'case DragEvent.ACTION_DROP:'.
  Here is my code (from Main.java):</p>
  
  <pre><code>public class Main extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          findViewById(R.id.ball1).setOnLongClickListener(listenClick);
          findViewById(R.id.ball1).setOnDragListener(listenDrag);
          findViewById(R.id.target1).setOnLongClickListener(listenClick);
          findViewById(R.id.target1).setOnDragListener(listenDrag);
          findViewById(R.id.target2).setOnDragListener(listenDrag);
          findViewById(R.id.target2).setOnLongClickListener(listenClick);
          findViewById(R.id.target3).setOnDragListener(listenDrag);
          findViewById(R.id.target3).setOnLongClickListener(listenClick);
  
  
  
  View.OnLongClickListener listenClick = new View.OnLongClickListener()
      {
          @Override
          public boolean onLongClick(View v)
          {
  
              ClipData data = ClipData.newPlainText("","");
              DragShadow dragShadow = new DragShadow(v);
  
              v.startDrag(data, dragShadow, v, 0);
  
              return false;
          }
      };
  
      private class DragShadow extends View.DragShadowBuilder
      {
          ColorDrawable greyBox;
  
          public DragShadow(View view)
          {
              super(view);
              greyBox = new ColorDrawable(Color.LTGRAY);
          }
  
          @Override
          public void onDrawShadow(Canvas canvas)
          {
              greyBox.draw(canvas);
          }
  
          @Override
          public void onProvideShadowMetrics(Point shadowSize,
                                             Point shadowTouchPoint)
          {
              View v = getView();
  
              int height = (int) v.getHeight();
              int width = (int) v.getWidth();
  
              greyBox.setBounds(0, 0, width, height);
  
              shadowSize.set(width, height);
  
              shadowTouchPoint.set((int)width/2, (int)height/2);
          }
      }
  
  
  
  View.OnDragListener listenDrag = new View.OnDragListener() {
  
          @Override
          public boolean onDrag(View v, DragEvent event)
          {
              int dragEvent = event.getAction();
  
              switch (dragEvent)
              {
                  case DragEvent.ACTION_DRAG_ENTERED:
                      Log.i("Drag Event", "Entered");
                      break;
  
                  case DragEvent.ACTION_DRAG_EXITED:
                      Log.i("Drag Event", "Exited");
                      break;
  
                  case DragEvent.ACTION_DROP:
                      ImageView target = (ImageView) v;
  
                      ImageView temp = (ImageView) event.getLocalState();
                      ImageView dragged = (ImageView) findViewById(temp.getId());
  
  
                      target.setId(dragged.getId());
                      target.setImageDrawable(dragged.getDrawable());
  
                      dragged.setId(temp.getId());
                      dragged.setImageDrawable(temp.getDrawable());
  
                      break;
  
              }
  
              return true;
          }
      };
  </code></pre>
  
  <p>If anyone can tell me why the setImageDrawable works on target, but not on dragged, I'll be very grateful.</p>