3

I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.

How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.

I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.

Any ideas of how to do this? Thanks!

K. Barresi
  • 1,275
  • 1
  • 21
  • 46

3 Answers3

4
public class MyActivity extends Activity {

private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    main = new LinearLayout(this);
    main.setOrientation(LinearLayout.VERTICAL);

    Button addButton = new Button(this);
    addButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            addEditText();
        }
    });

    Button submit = new Button(this);
    submit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            for (EditText editText : editTexts) {
                editText.getText().toString();
                // whatever u want to do with the strings
            }
        }
    });

    main.addView(addButton);
    main.addView(submit);
    setContentView(main);
}

private void addEditText() {
    LinearLayout editTextLayout = new LinearLayout(this);
    editTextLayout.setOrientation(LinearLayout.VERTICAL);
    main.addView(editTextLayout);

    EditText editText1 = new EditText(this);
    editText1.setId(id++);
    editTextLayout.addView(editText1);

    editTexts.add(editText1);

    EditText editText2 = new EditText(this);
    editText2.setId(id++);
    editTextLayout.addView(editText2);

    editTexts.add(editText2);

}
eoghanm
  • 329
  • 1
  • 9
  • Your code looks good, it could use a touch of automatic formatting and it's nice to have a sentence of to describing the codes function even with basic concepts. Also the user wants to have _two_ EditTexts added per `+` Button click, while you could simply add a nested LinearLayout, using a RelativeLayout would be better... – Sam Jul 16 '12 at 19:29
  • My bad I didn't really read the question fully, cheers anyway! – eoghanm Jul 16 '12 at 19:31
  • thanks eoghanm. One question: how would I collect the data from all the added editTexts? Is there something like a "getText() from all visible editTexts" function? – K. Barresi Jul 16 '12 at 19:35
  • As in: if I have a "submit" button, how do I go about getting data from all the forms? – K. Barresi Jul 16 '12 at 19:36
  • I have edited my comment now so each time an edit text is created its added to a list of edit texts, then in the submit button click listener it loops through the list of edit texts and gets the text thats in each of them – eoghanm Jul 16 '12 at 19:53
3

Do it in a ListView. Then you can just add them to a ListAdapter.

And then use adapter.notifyDatasetChanged()

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
AnAmuser
  • 1,885
  • 5
  • 26
  • 42
0

May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.

That view you can update into List View like inflating rows in the List View....

Srinivasan
  • 4,481
  • 3
  • 28
  • 36
  • Good point Sri. What I ended up doing was inflating an XML layout and using that instead of the individual edit texts – K. Barresi Jul 19 '12 at 14:00