title: Android : Adding EditText one below the old one dynamically
body: <p>I am working on an Android application in which I want to add <code>EditText</code> below an <code>EditText</code> which is at the first added in XML, and then added in code one below the other. Even though I am giving margin from top 200, the new EditText is appearing on top right. What am I doing wrong?</p>
  
  <p>XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/border"
      android:orientation="horizontal"
  
      &gt;
  
      &lt;RelativeLayout
          android:id="@+id/menuLayout"
          android:layout_width="wrap_content"
          android:layout_height="1000dp"&gt;
  
          &lt;ImageView
              android:layout_width="fill_parent"
              android:layout_height="180dp"
              android:background="@drawable/bottom_border"
              android:padding="3dp"
              android:scaleType="fitXY"
              android:src="@drawable/mittag_top" /&gt;
  
          &lt;EditText
              android:id="@+id/date"
              android:layout_width="wrap_content"
              android:layout_height="35dp"
              android:layout_alignParentEnd="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentStart="true"
              android:layout_alignParentTop="true"
              android:layout_marginLeft="80dp"
              android:layout_marginStart="80dp"
              android:layout_marginTop="125dp"
              android:hint="Menu karte datum..."
              android:lineSpacingExtra="10dp"
              android:lineSpacingMultiplier="1"
              android:paddingTop="7dp"
              android:textColor="@color/abc_primary_text_material_dark"
              android:textColorHint="@color/abc_primary_text_material_dark"
              android:textSize="12sp" /&gt;
  
          &lt;EditText
              android:id="@+id/time"
              android:layout_width="wrap_content"
              android:layout_height="35dp"
              android:layout_alignLeft="@+id/menuDate"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_alignStart="@+id/menuDate"
              android:layout_below="@+id/menuDate"
              android:hint="Opening times..."
              android:lineSpacingExtra="10dp"
              android:lineSpacingMultiplier="1"
              android:paddingTop="7dp"
              android:textColor="@color/abc_primary_text_material_dark"
              android:textColorHint="@color/abc_primary_text_material_dark"
              android:textSize="12sp" /&gt;
  
          &lt;EditText
              android:id="@+id/firstEntry"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentEnd="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentStart="true"
              android:layout_below="@+id/openTime"
              android:textColor="@color/abc_primary_text_material_dark"
              android:textColorHint="@color/abc_primary_text_material_dark"
              android:hint="Menu entry.." /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>Java code :</p>
  
  <pre><code>public class AddStuff extends Activity{
  
      EditText firstEntry, date, time;
      RelativeLayout relativeLayout;
      Typeface type;
      int counter = 1;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.ocr_menu);
  
  
          type = Typeface.createFromAsset(getAssets(),"fonts/MyFont.ttf");
          firstEntry = (EditText)findViewById(R.id.firstEntry);
          firstEntry.setTypeface(type);
          date = (EditText)findViewById(R.id.date);
          date.setTypeface(type);
          time = (EditText)findViewById(R.id.time);
          time.setTypeface(type);
          relativeLayout = (RelativeLayout)findViewById(R.id.menuLayout);
  
          firstEntry.addTextChangedListener(new TextWatcher(){
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  if (s.length() &gt; 0) {
                      addEditText();
                  }
              }
          });
  
      }
  
      public void addEditText(){
          EditText editText = new EditText(getApplicationContext());
          editText.setHint("Entry number "+counter);
         // editText.setPadding(2, 200, 2, 2);
          TableLayout.LayoutParams params = new TableLayout.LayoutParams();
          params.setMargins(2, 300, 0, 0);
          editText.setHintTextColor(Color.WHITE);
          editText.setTypeface(type);
          relativeLayout.addView(editText);
          counter++;
      }
  }
  </code></pre>
  
  <p>What am I doing wrong? How can I add EditText below the last added EditText. Thank you.</p>
  
  <p><em>Update</em></p>
  
  <p>updated addEdittext()</p>
  
  <pre><code>public void addEditText(){
          EditText editText = new EditText(getApplicationContext());
          editText.setHint("Entry number "+counter);
          RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                  RelativeLayout.LayoutParams.MATCH_PARENT,
                  RelativeLayout.LayoutParams.WRAP_CONTENT);
          int idLastChild= relativeLayout.getChildAt(relativeLayout.getChildCount()-1).getId();
          params.addRule(RelativeLayout.BELOW,idLastChild);
          editText.setHintTextColor(Color.WHITE);
          editText.setTypeface(type);
          relativeLayout.addView(editText);
  
          editText.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  if (s.length() &gt; 5) {
                      addEditText();
                  }
              }
          });
          counter++;
      }
  </code></pre>