title: Adding ImageViews dynamically in a Layout Android
body: <p>I'm trying to add dynamically some image views on a layout, because I have to move them in the same time, one after another, along a path.</p>
  
  <p>This is a part of my code, but I get this error: the specified child already has a parent. You must call removeView() on the child's parent first.</p>
  
  <p>I don't know how to solve this.</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_my);
  
          int random1=random(5);
          int random2=random(9);
  
  
          for(int i=1;i&lt;=random2;i++) {
  
              RelativeLayout layoutBase = (RelativeLayout) findViewById(R.id.banda_bk);
  
              ImageView Img = new ImageView(this);
  
              if (random1 == 1) {
                  Img = (ImageView) findViewById(R.id.ImageView1);
                  Img.setVisibility(View.VISIBLE);
                  anim_translate = AnimationUtils.loadAnimation(getApplicationContext(), translate);
                  Img.startAnimation(anim_translate);
              }
              if (random1 == 2) {
                  Img = (ImageView) findViewById(R.id.ImageView2);
                  Img.setVisibility(View.VISIBLE);
                  anim_translate = AnimationUtils.loadAnimation(getApplicationContext(), translate);
                  Img.startAnimation(anim_translate);
              }
              if (random1 == 3) {
                  Img = (ImageView) findViewById(R.id.ImageView3);
                  Img.setVisibility(View.VISIBLE);
                  anim_translate = AnimationUtils.loadAnimation(getApplicationContext(), translate);
                  Img.startAnimation(anim_translate);
              }
              if (random1 == 4) {
                  Img = (ImageView) findViewById(R.id.ImageView4);
                  Img.setVisibility(View.VISIBLE);
                  anim_translate = AnimationUtils.loadAnimation(getApplicationContext(), translate);
                  Img.startAnimation(anim_translate);
              }
              if (random1 == 5) {
                  Img = (ImageView) findViewById(R.id.ImageView5);
                  Img.setVisibility(View.VISIBLE);
                  anim_translate = AnimationUtils.loadAnimation(getApplicationContext(), translate);
                  Img.startAnimation(anim_translate);
              }
  
              layoutBase.addView(Img);
          }
      }
  </code></pre>
  
  <p>Thank you!</p>