title: Adding multiple image views programmatically
body: <p>I'm basically trying to achieve drag&amp;drop feature..</p>
  
  <p>What i'm trying is that i provides sequence of images on the screen, if i click on any image available in <code>images</code> row that will be added to the <code>Mains screen</code>. But i'm getting problem that when i add new view into the <code>Main Screen</code> then all the other <code>views</code> also moved to top left corner. </p>
  
  <p>Can you please tell me what is the problem...? Or kindly suggest me a tutorial or link where i can find solution.... or how to achieve this ?</p>
  
  <p>I'm using <code>Framelayout</code>, So that i also achieve images overlapping...</p>
  
  <p><img src="https://i.stack.imgur.com/YZjwJ.png" alt="enter image description here"></p>
  
  <p>This is the class in which all code is working:</p>
  
  <pre><code>public class drag extends Activity implements OnClickListener, OnTouchListener
  {
  ImageView img1;
  Button btn,btn2;
  FrameLayout layout;
  LayoutParams params;
  ImageView im , im2, im3 ,im4;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      layout = (FrameLayout) findViewById(R.id.vg);
      layout.setDrawingCacheEnabled(true);
      im  = (ImageView)findViewById(R.id.img1);
      im.setDrawingCacheEnabled(true);
      im.setOnTouchListener(this);
      im.setOnClickListener(this);
  
      btn  = (Button)findViewById(R.id.btn1);
      btn.setDrawingCacheEnabled(true);
      btn.setOnClickListener(this);
      btn.setOnTouchListener(this);
  
      btn2  = (Button)findViewById(R.id.btn2);
      btn2.setDrawingCacheEnabled(true);
      btn2.setOnClickListener(this);
      btn2.setOnTouchListener(this);
  
      params = new LayoutParams(LayoutParams.WRAP_CONTENT,
              LayoutParams.WRAP_CONTENT);
  
      btn.setOnClickListener(new OnClickListener()
      {
          @Override
          public void onClick(View v) 
          {
              im2 = new ImageView(drag.this);
              Bitmap bm  = BitmapFactory.decodeResource(getResources(), R.drawable.image);
              im2.setImageBitmap(bm);
              im2.setOnTouchListener(drag.this);
              im2.setOnClickListener(drag.this);
              layout.addView(im2, params);
  
          }
      });
  
      btn2.setOnClickListener(new OnClickListener()
      {
          @Override
          public void onClick(View v) 
          {
              Bitmap bm  = BitmapFactory.decodeResource(getResources(), R.drawable.image);                
              saveImage(bm);
  
          }
      });
  }
  
  public  void saveImage(Bitmap myBitmap) 
  {
      MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, "mmsImage" , "mmsimage");       
  }
  
  int l, t, r, b;
  int oldLeft, oldTop;
  PointF p, curr;
  
  @Override
  public boolean onTouch(View view, MotionEvent me) 
  {
  
      if (me.getAction() == MotionEvent.ACTION_DOWN) 
      {           
          //status = START_DRAGGING;
          Log.i("status"," AAA dOWN");
          img1 = new ImageView(this);
          view.setDrawingCacheEnabled(true);
          Bitmap mmsImage = Bitmap.createBitmap(view.getDrawingCache());
  
          img1.setImageBitmap(mmsImage);
          img1.setOnTouchListener(drag.this);
          img1.setOnClickListener(drag.this);
  
          oldLeft = (int)view.getLeft();
          oldTop = (int)view.getTop();
  
          p = new PointF(me.getRawX(), me.getRawY());
  
  
      }
      if (me.getAction() == MotionEvent.ACTION_MOVE) 
      {
          Log.i("status"," AAA draging");
  
          int xDiff = (int)(me.getRawX() - p.x);
          int yDiff  = (int)(me.getRawY() - p.y);
  
          p.x = me.getRawX();
          p.y = me.getRawY();
  
          l = view.getLeft();
          t = view.getTop();
          r = view.getRight();
          b = view.getBottom();
  
          view.layout(l + xDiff, t + yDiff , r + xDiff, b + yDiff);
  
      }
      if (me.getAction() == MotionEvent.ACTION_UP) 
      {
          Log.i("status"," AAA UP");
          //captureUserMove(view);
      } 
  
      return false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the XML : </p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/vg"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@android:color/white" &gt;
  
  &lt;ImageView
      android:id="@+id/img1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:src="@drawable/ic_launcher" /&gt;
  
  &lt;Button
      android:id="@+id/btn1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" /&gt;
  
  &lt;Button
      android:id="@+id/btn2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>