0

I am trying to add an editText dynamically to a relative layout. the layout contains an editText already. I need to add the new editText below the existing one.

EditText designation1 = new EditText(context);
designation1.setHint("designation");
designation1.setSingleLine(true);

and my layout is

layoutExp = 
    (RelativeLayout) childView.findViewById(R.id.relative_layout_edit_exp);

and my existing edit Text is

designation = (EditText)childView.findViewById(R.id.editTextDesignatn);
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Vikky
  • 933
  • 2
  • 15
  • 29

2 Answers2

3

You need to use RelativeLayout.LayoutParams and specifiy the relative position with the addRule(RelativeLayout.BELOW, ...) (this is the programmatic equivalent for android:below XML attribute):

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.editTextDesignatn);
layoutExp.addView(designation1, params);
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • i using a button click for adding text boxes dynamically. but the button click responds only for ist time. the 2nd time it never create the editText – Vikky Jan 18 '13 at 08:18
  • it's probably created at the same position, that's why you don't see it – sdabet Jan 18 '13 at 08:20
0

It will be possible if You set for example a LinearLayout under Your existing EditText. Give an ID to this LinearLayout and then You could put Your dynamic EditText to this LinearLayout like

    YourLinearLayout.addView(YourDynamicEditText);
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49