title: Having trouble removing and then adding views programatically to FrameLayout
body: <p>I am having trouble removing a LinearLayout from a FrameLayout, to then add a new LinearLayout that is populated with EditText generated programatically.</p>
  
  <p>The first time that the view is loaded, I catch a reference to the FrameLayout using findViewById(), and then I make a new LinearLayout programmatically and add a number of EditText to it.
  It looks like this</p>
  
  <pre><code>formPlace = (FrameLayout) findViewById(R.id.form_place);
  
  LinearLayout linearView = new LinearLayout(this);
  linearView = makeEditText(arrayItems,category);
  linearView.setBackgroundColor(Color.GRAY);
  linearView.setOrientation(LinearLayout.VERTICAL);
  
  formPlace.addView(linearView);
  </code></pre>
  
  <p>Next is the function that makes the EditTexts:</p>
  
  <pre><code>private LinearLayout makeEditText(ArrayList&lt;String[]&gt; arrayString, int position){
          LinearLayout view = new LinearLayout(this);
          arrayInputs = new ArrayList&lt;&gt;();
          String[] arrayHints = arrayString.get(position);
          ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  
          for (int i=0;i&lt;arrayHints.length;i++){
              EditText editText = new EditText(this);
              Log.i("myApp","tantas veces" + i);
  
              editText.setId(i);
              editText.setTextColor(Color.BLACK);
              editText.setHint(arrayHints[i]);
              editText.setHintTextColor(Color.GRAY);
  
  
              editText.setLayoutParams(params);
  
              view.addView(editText);
              arrayInputs.add(editText);
  
          }
          LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
          buttonParams.gravity = Gravity.RIGHT;
  
          android.widget.Button save = new android.widget.Button(this);
          save.setText("Guardar");
          save.setLayoutParams(buttonParams);
          save.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Log.i("myApp", "guardar");
                  saveClicked(category);
              }
          });
  
          view.addView(save);
  
          android.widget.Button cancel = new android.widget.Button(this);
          cancel.setText("cancelar");
          cancel.setLayoutParams(buttonParams);
          cancel.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Log.i("myApp", "cancelar");
                  cancelClicked();
              }
          });
  
          view.addView(cancel);
          return view;
      }
  </code></pre>
  
  <p>Then, when a spinner event is called, I catch the reference of the FrameLayout and pass .removeAllViews(). This works as intended, and my pevious view is deleted. The problem comes when I try to add new views to the FrameLayout in the makeEditText line:</p>
  
  <pre><code>spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                  @Override
                  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, final int position, long id) {
  
                  if(firstTime==false) {
                      FormActivity.this.formPlace.removeAllViews();
  
                      LinearLayout auxLinearView = new LinearLayout(FormActivity.this);
                      auxLinearView = makeEditText(arrayItems, position);
                      FormActivity.this.formPlace.addView(auxLinearView);
                  }
                  firstTime =false;
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
              }
  
          }); 
  </code></pre>
  
  <p>What happens is that the old view is deleted but the new view is not set properly. It only shows 1 element of the 4 is intended to show, and the buttons that I create programmatically don't show at all. And my FrameLayout occupies all the remaining space in my screen, even though is set to wrap content. What it implies is that the view is not behaving as it should.</p>
  
  <p>EDIT:</p>
  
  <p>Here are some Screenshots</p>
  
  <p>Here is how it looks before removeAllViews():</p>
  
  <p><a href="https://dl.pushbulletusercontent.com/tExAXJexe4BfG5sGuKvb1RFtGOFd51nE/Screenshot_2015-05-04-17-54-18.png" rel="nofollow">https://dl.pushbulletusercontent.com/tExAXJexe4BfG5sGuKvb1RFtGOFd51nE/Screenshot_2015-05-04-17-54-18.png</a></p>
  
  <p>Here is how it looks after adding the views to the FrameLayout:</p>
  
  <p><a href="https://dl.pushbulletusercontent.com/JM72D3ZrLwTh0YC9g7PODREyOfCj667P/Screenshot_2015-05-04-17-54-25.png" rel="nofollow">https://dl.pushbulletusercontent.com/JM72D3ZrLwTh0YC9g7PODREyOfCj667P/Screenshot_2015-05-04-17-54-25.png</a></p>