How to put EditText next to Edit Text dynamically?

3

I am creating a dynamic form and needed to display on a form, Age and Address on the same line. I created a "for" to create n times the same form but it is giving error. follow the code:

public void form() {

    for (int i = 0; i < 3; i++) {

        TextView tv = new TextView(this);
        tv.setText("Nome");
        layout.addView(tv);
        EditText et = new EditText(this);
        et.setText("");
        layout.addView(et);

        TextView tv1 = new TextView(this);
        tv1.setText("Idade");
        layout2.addView(tv1);
        EditText et1 = new EditText(this);
        et1.setText("");
        layout2.addView(et1);

        TextView tv2 = new TextView(this);
        tv2.setText("Endereco");
        layout2.addView(tv2);
        EditText et2 = new EditText(this);
        et2.setText("");
        layout2.addView(et2);
        // AQUI COLOCO O LAYOUT HORIZONTAL DENTRO DO VERTICAL, FUNCIONA A LÓGICA, PORÈM ESTÁ DANDO ERRO POR CAUSA DO FOR
        layout.addView(layout2);

        TextView tv3 = new TextView(this);
        tv3.setText("Telefone");
        layout.addView(tv3);
        EditText et3 = new EditText(this);
        et3.setText("");
        layout.addView(et3);

    }

}

Note: layout - > Vertical LinearLayout      layout2 - > Horizontal LinearLayout.

If I take the "for" code, it works, but I need to use the "for". Someone can help. Thank you in advance !!!

    
asked by anonymous 11.09.2015 / 15:31

1 answer

1

A view can not be added 2 times within another view. You have to make the creation of layout2 dynamic.

To do this you can create this view in a separate xml and inflate every time you want a new instance.

    
13.12.2016 / 14:10