0

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++;
    }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • You have to add the ```RelativeLayout.LayoutParams``` so that the EditText will be positioned below the older one. I suggest to use a ```LinearLayout``` with vertical orientation, this will add the children one below older one and so on. – danypata Jul 12 '16 at 10:42
  • set it below your `firstEntry` EditText programmatically, refer [Can I set “android:layout_below” at runtime, programmatically?](http://stackoverflow.com/questions/3277196/can-i-set-androidlayout-below-at-runtime-programmatically) – Ravi Jul 12 '16 at 10:43

4 Answers4

1

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);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • What after the first EditText is added, any other EditText will be added below it, as I will be adding a TextWatcher and adding more EditText below the already added one. – We are Borg Jul 12 '16 at 10:46
  • Just one last thing, how can I add an EditText below the one which is added in your code, something similar to what I am doing with TextWatcher and firstEntry, where I add an EditText, I would like to that same for newly added EditText. Thank you very much. – We are Borg Jul 12 '16 at 10:49
  • @WeareBorg: Your problem is not clear to me. because `relativeLayout.getChildCount()-1` will return always last EditText which is in `RelativeLayout ` so we are adding new EditText after that. please explain more – ρяσѕρєя K Jul 12 '16 at 10:52
  • Yes. So, the use-case is, the user can enter multiple texts in each of the EditText, but as the user starts typing in one, there should be an empty one added dynamically below, in which then the user can type and so on... Any doubts, please let me know. Thank you. – We are Borg Jul 12 '16 at 10:53
  • @WeareBorg: yes right , this is current flow when `if (s.length() > 0) {` condition is true then a new EditText added in dynamically below . Now what you want? – ρяσѕρєя K Jul 12 '16 at 10:56
  • I have created a chatroom : http://chat.stackoverflow.com/rooms/117072/discussion-between-we-are-borg-and--k .. I have also updated my main post, have added the updated addeditText() mechanism, in which I will recursively call addEditText again, is it correct? Anyhow, any newly addded EditText is still at top right, not below the last child. – We are Borg Jul 12 '16 at 11:01
  • Thanks a lot for your help. Enjoy. Tc. :-) – We are Borg Jul 12 '16 at 11:31
0

It's a relative layout. Not LinearLayout. You should specify how position of your view depends on other views.

RelativeLayout.LayoutParams params
            = (RelativeLayout.LayoutParams) yourView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.firstEntry);
thealeksandr
  • 1,686
  • 12
  • 17
0

You're creating the params, but not assigning them to the layout. Do as follow:

EditText editText = new EditText(getApplicationContext());
final LayoutParams lparams = new LayoutParams(50,30); // Width , height
editText.setLayoutParams(lparams);
Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
0

Try this

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.LayoutParams newParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);

   newParams.addRule(RelativeLayout.BELOW, R.id.date);

    editText.setLayoutParams(newParams);

  relativeLayout.addView(editText);
    counter++;
}
Arjun saini
  • 4,223
  • 3
  • 23
  • 51