title: Android add edittext field on click of (+) button and remove by click of (-)
body: <p>I am creating a add contact activity. There will be only one edit text and a button (+), when click on button then another edit text should be added and previous button should change into (-). By this method i want to add and remove edit text fields.<a href="http://www.google.co.in/imgres?q=android%20add%20contact&amp;hl=en&amp;safe=active&amp;tbo=d&amp;noj=1&amp;tbm=isch&amp;tbnid=SDkjnonb2GU7GM:&amp;imgrefurl=https://stackoverflow.com/questions/9732783/android-add-contact-form-dynamically&amp;docid=4NU7riH2qdlSVM&amp;imgurl=http://i.stack.imgur.com/CCmqj.jpg&amp;w=640&amp;h=572&amp;ei=eRgiUYvYFITrrQeqoICIBA&amp;zoom=1&amp;ved=1t:3588,r:3,s:0,i:90&amp;iact=rc&amp;dur=1806&amp;sig=111728917285653940940&amp;page=1&amp;tbnh=171&amp;tbnw=191&amp;start=0&amp;ndsp=24&amp;tx=76&amp;ty=79&amp;biw=1366&amp;bih=664" rel="nofollow noreferrer">Some thing like this.</a> I am doing like this but not able to achieve that i want. Any idea?</p>
  
  <pre><code>public class TextField extends Activity {
  
  Context con;
  TableLayout table;
  TableRow tr[] = new TableRow[6];
  EditText txt[] = new EditText[6];
  //ImageView img[] = new ImageView[5];
  Button img[]=new Button[6];
  int count ;
  int lastDeletedIndex;
  int lastPopulatedIndex;
  boolean isDel;
  public void onCreate(Bundle obj) {
      super.onCreate(obj);
      // Log.d("","On Create");
      con = this;
      count = 1;
  
      table = new TableLayout(this);
      tr[count] = new TableRow(this);
      tr[count].setId(count);
      tr[count].setLayoutParams(new TableRow.LayoutParams(    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  
      txt[count] = new EditText(this);
      txt[count].setId(count);
      txt[count].setText("1");
      txt[count].setMaxLines(1);
      txt[count].setWidth(160);
  
          img[count] = new Button(this);
          img[count].setId(count);
          img[count].setText("+");
          img[count].setBackgroundResource(R.drawable.add);
          img[count].setPadding(5, 7, 20, 0);
  
  
      tr[count].addView(txt[count]);
      tr[count].addView(img[count]);
      tr[count].setPadding(20, 20, 0, 0);
  
      img[count].setOnClickListener(new OnClick(img[count]));
  
      table.addView(tr[count]);
      setContentView(table);
  
  }
  
  class OnClick implements OnClickListener {
  
      View view;
  
      Button addIcon;
  
      public OnClick(View view) {
  
          this.addIcon = (Button) view;
  
      }
  
      @Override
      public void onClick(View arg0) {
  
              int id=addIcon.getId();
          if(addIcon.getText().toString().equals("-"))
          {
  
              if ((id &gt;1  )) {
                  isDel=true;
                  count=id;
                  lastDeletedIndex=id;
                  txt[count]=null;
                  img[count]=null;
                  table.removeView(tr[count]);
                  tr[count]=null;
                  Log.d("", "Cancel img Id" + addIcon.getId());
                  }       
  
  
          } else {
              if (count &lt;5) {
                  if(table.getChildCount()!=5)
                  {
                      addIcon.setBackgroundDrawable(getResources().getDrawable(R.drawable.delete));
                      addIcon.setText("-");
  
  
                  addIcon.setId(count);
                  if(!isDel) {
                      count++;
                  }else
                  {
                      count=lastDeletedIndex;
                  }
  
  
                  tr[count] = new TableRow(con);
                  tr[count].setId(count);
  
  
                      img[count] = new Button(con);
                  img[count].setId(count);
                  img[count].setBackgroundDrawable(getResources().getDrawable(R.drawable.add));
                  img[count].setText("+");
                  img[count].setPadding(3, 7, 20, 0);
                  img[count].setOnClickListener(new OnClick(img[count]));
  
                  txt[count] = new EditText(con);
                  txt[count].setId( count);
                  txt[count].setText(""+count);
                  txt[count].setMaxLines(1);
                  txt[count].setWidth(160);
  
                  tr[count].addView(txt[count]);
  
                  tr[count].addView(img[count]);
  
                  tr[count].setPadding(20, 20, 0, 0);
                  table.addView(tr[count]);
                  setContentView(table);
                  isDel=false;
  
                  lastPopulatedIndex=count;
  
                  Log.d("", "Cancel img Id" + addIcon.getId());
                  }
              } else
                  ShowDialog.showAlert("Info","You can't add more than 5 contacts!", con);
          }
  
      }
  }
  </code></pre>