title: Android Drag and drop images on the Screen?
body: <p>I m working on project user to move the image in one position to Another position on the screen. I have written a sample code to move the image but the problem here is if I move one image the neighbouring image also starts moving.. Here is the sample code.any one Idea of this. </p>
  
  <p>Main.java</p>
  
  <pre><code>public class MainActivity extends Activity  {
     int windowwidth;
     int windowheight;    
     ImageView ima1,ima2;
  
     private android.widget.RelativeLayout.LayoutParams layoutParams ;
     // private android.widget.RelativeLayout.LayoutParams layoutParams ;
     //private android.widget.RelativeLayout.LayoutParams layoutParams ;           
  
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
  
           windowwidth = getWindowManager().getDefaultDisplay().getWidth();
           windowheight = getWindowManager().getDefaultDisplay().getHeight();
  
           System.out.println("width" +windowwidth);
           System.out.println("height" +windowheight);             
  
           ima1 = (ImageView)findViewById(R.id.imageview1);
           ima1.setOnTouchListener(new View.OnTouchListener() {  
  
  public boolean onTouch(View v, MotionEvent event) {
         layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();
  
           switch(event.getAction())                   
              {
                case MotionEvent.ACTION_DOWN:                          
                      break;     
  
                case MotionEvent.ACTION_MOVE:
                      int x_cord = (int) event.getRawX();
                      int y_cord = (int) event.getRawY();
  
                System.out.println("value of x" +x_cord);
                System.out.println("value of y" +y_cord);           
  
                      if (x_cord &gt; windowwidth) {
                          x_cord = windowwidth;
                         }
                      if (y_cord &gt; windowheight) {
                          y_cord = windowheight;
                         }
               layoutParams.leftMargin = x_cord-25;
               layoutParams.topMargin = y_cord-25;
               //   layoutParams.rightMargin = x_cord-25;
               //   layoutParams.bottomMargin = y_cord-25;
               ima1.setLayoutParams(layoutParams);
                       break;
                 default: break;
                }  
                 return true;
              }
           });
  
           ima2 = (ImageView)findViewById(R.id.imageview2);
           ima2.setOnTouchListener(new View.OnTouchListener() {         
  
       public boolean onTouch(View v, MotionEvent event) {
           layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
                switch(event.getActionMasked())
                   {
                     case MotionEvent.ACTION_DOWN:
                         break;
                     case MotionEvent.ACTION_MOVE:
                         int x_cord = (int) event.getRawX();
                         int y_cord = (int) event.getRawY();
  
                         System.out.println("value of x1" +x_cord);
                     System.out.println("value of y1" +y_cord);                            
  
                          if (x_cord &gt; windowwidth) {
                              x_cord = windowwidth;
                          }
                          if (y_cord &gt; windowheight) {
                              y_cord = windowheight;
                          }
                          layoutParams.leftMargin = x_cord - 25;
                          layoutParams.topMargin = y_cord - 75;
                          ima2.setLayoutParams(layoutParams);
                          break;
                      default: break;
                  }
                  return true;
              }
          });
         }
     }
  </code></pre>
  
  <p>main.xml</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;RelativeLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"&gt;
    &lt;ImageView 
      android:layout_width="100dp" 
      android:layout_height="100dp"
      android:id="@+id/imageview1" 
      android:src="@drawable/image1"  /&gt;    
  &lt;ImageView
      android:layout_width="100sp" 
      android:layout_height="100sp" 
      android:id="@+id/imageview2"
      android:src="@drawable/image2"   /&gt;             
   &lt;/RelativeLayout&gt;
  </code></pre>