title: How to add TextWatcher on Editext which is created at Runtime (programmatically)?
body: <p>I want to use TextWatcher on EditText which is created at Runtime programmatically. But it is not working with EditText created at Runtime.</p>
  
  <p>I used Model class for UI elements to be add at Runtime. And a Controller class having arraylist to store objects of Model class, my code is below,</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      EditText editText_A;
      RelativeLayout layout_root;
      Controller aController;
      int counter = 1;
      int id = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // inflate UI
          layout_root = (RelativeLayout) findViewById(R.id.layout_root);
          editText_A = (EditText) findViewById(R.id.editText_A);
  
          // controller object
          aController = (Controller) getApplication();
  
          // set last UI element into list
          aController.get_ui_list().add(new UI_Model(editText_A.getId()));
  
          // dynamic button
          Button add = new Button(this);
          Button remove = new Button(this);
  
          // set id/Text
          add.setId(id);
          remove.setId(++id);
          add.setText("ADD");
          remove.setText("REMOVE");
  
          // setting rules
          final LayoutParams add_param = new LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT);
          add_param.addRule(RelativeLayout.ALIGN_LEFT, aController
                  .get_UI_lastItem().getId());
          add_param.addRule(RelativeLayout.BELOW, aController.get_UI_lastItem()
                  .getId());
  
          final LayoutParams remove_param = new LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT);
          remove_param.addRule(RelativeLayout.ALIGN_RIGHT, aController
                  .get_UI_lastItem().getId());
          remove_param.addRule(RelativeLayout.BELOW, aController
                  .get_UI_lastItem().getId());
          layout_root.addView(add, add_param);
          layout_root.addView(remove, remove_param);
  
          add.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // add UI
                  add_UI();
                  // set button at last
                  add_param.addRule(RelativeLayout.ALIGN_LEFT, aController
                          .get_UI_lastItem().getId());
                  add_param.addRule(RelativeLayout.BELOW, aController
                          .get_UI_lastItem().getId());
                  remove_param.addRule(RelativeLayout.ALIGN_RIGHT, aController
                          .get_UI_lastItem().getId());
                  remove_param.addRule(RelativeLayout.BELOW, aController
                          .get_UI_lastItem().getId());
  
              }
          });
          remove.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // remove UI
                  remove_UI();
                  // set button at last
                  add_param.addRule(RelativeLayout.ALIGN_LEFT, aController
                          .get_UI_lastItem().getId());
                  add_param.addRule(RelativeLayout.BELOW, aController
                          .get_UI_lastItem().getId());
                  remove_param.addRule(RelativeLayout.ALIGN_RIGHT, aController
                          .get_UI_lastItem().getId());
                  remove_param.addRule(RelativeLayout.BELOW, aController
                          .get_UI_lastItem().getId());
              }
          });
  
          // Text watcher
          TextWatcher general_text_watcher = new TextWatcher() {
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
                  // default UI
                  if (editText_A.getText().toString().trim().length() == 0) {
                      editText_A.setText("0");
                  }
                  // dynamic UI
                  if (counter &gt; 1) {
                      for (int i = 1; i &lt; counter; i++) {
  
                          if (aController.get_ui_list().get(i).getEd1().getText()
                                  .toString().trim().length() == 0) {
                              aController.get_ui_list().get(i).getEd1()
                                      .setText("0");
                          }
                      }
                  }
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
              }
  
              @Override
              public void afterTextChanged(Editable s) {
              }
          };
          // add watcher
          editText_A.addTextChangedListener(general_text_watcher);
          // dynamic UI
          if (counter &gt; 1) {
              for (int i = 1; i &lt; counter; i++) {
                  aController.get_ui_list().get(i).getEd1()
                          .addTextChangedListener(general_text_watcher);
              }
          }
      }
  
      // add UI elements
      public void add_UI() {
  
          EditText ed_temp = new EditText(this);
          // set id/Text
          ed_temp.setId(++id);
          ed_temp.setEms(10);
  
          // set rule
          LayoutParams ed_temp_param = new LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT);
          ed_temp_param.addRule(RelativeLayout.ALIGN_LEFT, aController
                  .get_UI_lastItem().getId());
          ed_temp_param.addRule(RelativeLayout.BELOW, aController
                  .get_UI_lastItem().getId());
          // add view
          layout_root.addView(ed_temp, ed_temp_param);
          // add model object
          UI_Model obj = new UI_Model(ed_temp, ed_temp.getId());
          aController.set_ui_listItem(obj);
  
          id++;
          counter++;
      }
  
      // remove UI elements
      public void remove_UI() {
          counter--;
          if (aController.get_ui_list().size() &gt; 1) {
              layout_root.removeView(aController.get_UI_lastItem().getEd1());
  
              // update array list
              aController.get_ui_list().remove(
                      aController.get_ui_list().size() - 1);
          } else {
              Toast.makeText(getApplicationContext(), "cannot remove", 1000)
                      .show();
          }
      }
  }
  </code></pre>
  
  <p>Controller.java</p>
  
  <pre><code>public class Controller extends Application {
  
      // array list Model class
      ArrayList&lt;UI_Model&gt; ui_list = new ArrayList&lt;UI_Model&gt;();
  
      //ArrayList&lt;Data_Model&gt; data_list = new ArrayList&lt;Data_Model&gt;();
  
      public ArrayList&lt;UI_Model&gt; get_ui_list() {
          return ui_list;
      }
  
      public void set_ui_list(ArrayList&lt;UI_Model&gt; ui_list) {
          this.ui_list = ui_list;
      }
  
      public UI_Model get_UI_lastItem() {
          return ui_list.get(ui_list.size() - 1);
      }
  
      public void set_ui_listItem(UI_Model item){
          ui_list.add(item);
      }
  }
  </code></pre>
  
  <p>UI_Model.java</p>
  
  <pre><code>public class UI_Model {
  
      EditText ed1;
      int id;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public UI_Model(EditText ed1, int id) {
          super();
          this.ed1 = ed1;
          this.id = id;
      }
  
      public UI_Model(int id) {
          super();
          this.id = id;
      }
  
      public EditText getEd1() {
          return ed1;
      }
  
      public void setEd1(EditText ed1) {
          this.ed1 = ed1;
      }
  }
  </code></pre>
  
  <p>Above is my full code, my question is,</p>
  
  <p>Why TextWatcher is not working on dynamic UI created at Runtime, while working on static layout UI?</p>