Question:
I'm creating a DynaForm and needed to display in a form, Age and Address on the same line. I created a "for" to create n times the same form but it is giving an 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 -> LinearLayout vertical layout2 -> LinearLayout horizontal.
If I take the "for" out of the code, it works, but I need to use the "for". Can anyone help. Thanks in advance!!!
Answer:
A view cannot be added twice within another view. You have to make the layout2 creation dynamic.
To do this you can create this view in a separate xml and inflate each time you want a new instance.