title: Add view programmatically to RelativeLayout android
body: <p>how can I do to add child view programmatically to RelativeLayout at a given position?</p>
  
  <p>For example, to reflect the following picture:</p>
  
  <p><img src="https://i.stack.imgur.com/55K4V.jpg" alt="enter image description here"></p>
  
  <p>my xml: (com.example.TransparentSlidingDrawer child is a simple class that extends with LinearLayout)</p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_centerVertical="true"
      android:orientation="horizontal" &gt;
   &lt;ScrollView 
          android:layout_width="wrap_content"
          android:layout_height="100dp"
          &gt;
  &lt;com.example.TransparentSlidingDrawer
      android:id="@+id/popup"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
       &gt;
       &lt;RelativeLayout 
           android:id="@+id/relativelayout_imageview_textviews_slider"
           android:layout_width="wrap_content"
           android:layout_height="100dp"
           &gt;    
       &lt;/RelativeLayout&gt;
  
  
  &lt;/com.example.TransparentSlidingDrawer&gt;
  
  &lt;/ScrollView&gt; 
  &lt;ImageButton 
      android:id="@+id/open_close_slider"
      android:layout_width="25dp"
      android:layout_height="25dp"
      /&gt;
  </code></pre>
  
  <p>    </p>
  
  <hr>
  
  <p>this is my function that this function does not work well :</p>
  
  <pre><code>private void addChild(){
      RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayout_imageview_textviews_slider);
  
      RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  
      imageparams.setMargins(0, 5, 0, 0);
  
      RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  
      textparams.setMargins(0, 25, 5, 0);
  
      imageparams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
      imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  
      for(int i = 0 ; i &lt; 9 ; ++i){
          ImageButton btn = new ImageButton(getApplicationContext());
          btn.setId(i+1);
          btn.setBackgroundResource(R.drawable.ic_launcher);
          TextView txt = new TextView(getApplicationContext());
          txt.setText("text"+i);
          txt.setId(i+1);
          textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
          textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());
          relativelayout.addView(btn, imageparams);
          relativelayout.addView(txt, textparams);
      }
  
  }
  </code></pre>