title: Android - removing layout programmatically
body: <p>I have an activity that makes a layout programmatically from a Shared Preference using a for loop. The text views and buttons are enclosed in a linear layout. The user can input as many views as he wants. Now, the button will be a delete button. When pressed, I want to delete the linear layout the button and the other textviews are contained. How do I do this?</p>
  
  <p>HERE IS MY CODE:</p>
  
  <pre><code>package com.dirkjan.myschools;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.content.res.Configuration;
  import android.graphics.Typeface;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
  LinearLayout subjectLeft, subjectRight;
  
  Button addSubj;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      subjectLeft = (LinearLayout) findViewById(R.id.llSubjectLeft);
      subjectRight = (LinearLayout) findViewById(R.id.llSubjectRight);
  
      //Load the saved subjects
      SharedPreferences getSubjects = getSharedPreferences("SubjectInfo_Prefs", MODE_PRIVATE);
      SharedPreferences.Editor editor = getSubjects.edit();
  
      int subjectCount = getSubjects.getInt("count", 0);
      if (subjectCount &gt; 0 ){
          for (int i = 1; i &lt;= subjectCount; i++){
              //Set the linear layout for each subject
              LinearLayout ll = new LinearLayout(this);
              ll.setOrientation(LinearLayout.VERTICAL);
              LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  
              float scale = getResources().getDisplayMetrics().density;
              //SET BOTTOM MARGIN
              float margin = 5; //RESIZE MARGIN HERE!
              int margs = (int) (margin * scale + 0.5f);
  
              //SET PADDING IN DP
              float padding = 5; //RESIZE PADDING HERE!
              int pads = (int) (padding * scale +0.5f);
              llParams.setMargins(0,0,0,margs);
  
              //SETTING THE LINEARLAYOUT PARAMS
              ll.setLayoutParams(llParams);
              ll.setPadding(pads, pads, pads, pads);
  
              //SETTING THE BACKGROUND COLOR OF THE LINEAR LAYOUT
              String chosenColor = getSubjects.getString("chosenColor" + i, "BLUE");
  
              if (chosenColor.equals("Green")){
                  ll.setBackgroundResource(R.color.HoloGreen);
              }else if (chosenColor.equals("Blue")){
                  ll.setBackgroundResource(R.color.HoloBlue);
              }else if (chosenColor.equals("Gray")){
                  ll.setBackgroundResource(R.color.HoloGray);
              }else if (chosenColor.equals("Orange")){
                  ll.setBackgroundResource(R.color.HoloOrange);
              }else {
                  ll.setBackgroundResource(R.color.HoloYellow);
              }
  
              //ADDING THE LAYOUT TO THE APPROPRIATE CONTAINER (LEFT OR RIGHT)
              if (i % 2 == 1){
                  subjectLeft.addView(ll);
              } else {
                  subjectRight.addView(ll);
              }
  
              //SETTING THE SUBJECT NAME TEXTVIEW
              TextView SubjectName = new TextView(this);
              SubjectName.setText(getSubjects.getString("subjectName" + i, "Error"));
              SubjectName.setLayoutParams(new ViewGroup.LayoutParams(
                      ViewGroup.LayoutParams.MATCH_PARENT,
                      ViewGroup.LayoutParams.WRAP_CONTENT));
              SubjectName.setTextSize(22);
              SubjectName.setTypeface(Typeface.DEFAULT_BOLD);
  
              //SETTING THE SUBJECT NUMB TEXT VIEW
              TextView SubjectNumber = new TextView(this);
              SubjectNumber.setText(getSubjects.getString("subjectNumb" + i, "Error"));
              SubjectNumber.setLayoutParams(new ViewGroup.LayoutParams(
                      ViewGroup.LayoutParams.MATCH_PARENT,
                      ViewGroup.LayoutParams.WRAP_CONTENT));
              SubjectNumber.setTextSize(16);
  
              //Creating the divider line
              ImageView divider = new ImageView(this);
              LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2);
              divider.setLayoutParams(dividerParams);
              divider.setBackgroundResource(R.color.Black);
  
              //Add Views into the Layout
              ll.addView(SubjectNumber);
              ll.addView(SubjectName);
              ll.addView(divider);
          }
  
  
  
      }
  
      addSubj = (Button) findViewById(R.id.buttonPlusSubject);
      addSubj.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              Intent toAddSubj = new Intent(MainActivity.this,
                      AddSubjectActivity.class);
              startActivity(toAddSubj);
              finish();
          }
      });
  }
  
  
  
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  }
  </code></pre>
  
  <p>Please do take note that no id is assigned for each layout. It would help if there is a code to identify the parent of the  parent of the button (The button is in a relative layout, which is in a linear layout where the linear layout must be removed by clicking the button.</p>