Search code examples
javaandroiduser-interfaceandroid-edittext

Android : Adding EditText one below the old one dynamically


I am working on an Android application in which I want to add EditText below an EditText 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?

XML file:

<?xml version="1.0" encoding="utf-8"?>
<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"

    >

    <RelativeLayout
        android:id="@+id/menuLayout"
        android:layout_width="wrap_content"
        android:layout_height="1000dp">

        <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" />

        <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" />

        <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" />

        <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.." />
    </RelativeLayout>
</ScrollView>

Java 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() > 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++;
    }
}

What am I doing wrong? How can I add EditText below the last added EditText. Thank you.

Update

updated addEdittext()

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() > 5) {
                    addEditText();
                }
            }
        });
        counter++;
    }

Solution

  • Because adding dynamic EditText in RelativeLayout so need to use LayoutParams .addRule instead of margin to align EditText:

        EditText editText = new EditText(AddStuff.this);
        ... 
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.MATCH_PARENT, 
                                           RelativeLayout.LayoutParams.WRAP_CONTENT);
    
    // get last child id from RelativeLayout
     int idLastChild=relativeLayout.getChildAt(relativeLayout.getChildCount()-1);
     params.addRule(RelativeLayout.BELOW,idLastChild);
      // set id for EditText 
     editText.setId(idLastChild+1);
     // set layout params
     editText.setLayoutParams(params);
     relativeLayout.addView(editText, params);