title: Add spinner with click of a button dynamically in Android
body: <p>I was trying to add spinner to my app dynamically by using the following code:</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      startTime = (EditText)findViewById(R.id.EditTextST);  
      startDate = (EditText)findViewById(R.id.editTextSD); 
  
      LinearLayout linearlayout = new LinearLayout(this);
      //RelativeLayout linearlayout = (RelativeLayout)findViewById(R.id.main_layout);
  
      ArrayList&lt;String&gt; spinnerArray = new ArrayList&lt;String&gt;();
      spinnerArray.add("one");
      spinnerArray.add("two");
      spinnerArray.add("three");
      spinnerArray.add("four");
      spinnerArray.add("five");
  
      Spinner spinner = new Spinner(this);
      ArrayAdapter&lt;String&gt; spinnerArrayAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
      spinner.setAdapter(spinnerArrayAdapter);
  
      linearlayout.addView(spinner);
  
      setContentView(linearlayout);
  }
  </code></pre>
  
  <p>This adds the spinner but replaces everything I have in my XML with the dynamically-created spinner.
  I want to add the spinner to the layout (which I have created by using XML) by clicking a button, not replacing the layout. Please help. </p>
  
  <p>EDIT: Here is my XML file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  
  &lt;RelativeLayout
      android:id="@+id/main_layout"
      android:layout_width="match_parent"
      android:layout_height="900dp" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:text="@string/select_week"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/textView1"
          android:text="@string/sun" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/checkBox1"
          android:layout_alignBottom="@+id/checkBox1"
          android:layout_toRightOf="@+id/checkBox1"
          android:text="@string/mon" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBottom="@+id/checkBox2"
          android:layout_toRightOf="@+id/checkBox2"
          android:text="@string/tue" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/checkBox3"
          android:layout_alignBottom="@+id/checkBox3"
          android:layout_toRightOf="@+id/checkBox3"
          android:text="@string/wed" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox5"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/checkBox1"
          android:layout_toLeftOf="@+id/checkBox2"
          android:text="@string/thu" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox6"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/checkBox2"
          android:layout_below="@+id/checkBox2"
          android:text="@string/fri" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox7"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/checkBox2"
          android:layout_toRightOf="@+id/checkBox2"
          android:text="@string/sat" /&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/checkBox5"
          android:layout_marginTop="18dp"
          android:text="@string/select_week_number"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;CheckBox
          android:id="@+id/CheckBox01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/checkBox02"
          android:layout_alignBottom="@+id/checkBox02"
          android:layout_alignLeft="@+id/checkBox5"
          android:text="@string/one" /&gt;
  
      &lt;CheckBox
          android:id="@+id/CheckBox03"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/checkBox02"
          android:layout_alignBottom="@+id/checkBox02"
          android:layout_toRightOf="@+id/checkBox6"
          android:text="@string/three" /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox02"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textView2"
          android:layout_toRightOf="@+id/checkBox5"
          android:text="@string/two" /&gt;
  
      &lt;CheckBox
          android:id="@+id/CheckBox04"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/CheckBox03"
          android:layout_alignBottom="@+id/CheckBox03"
          android:layout_toRightOf="@+id/textView2"
          android:text="@string/four" /&gt;
  
      &lt;TextView
          android:id="@+id/TextView01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/CheckBox01"
          android:layout_marginTop="16dp"
          android:text="@string/select_start_date"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;EditText
          android:id="@+id/editTextSD"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/CheckBox01"
          android:layout_below="@+id/TextView01"
          android:layout_toLeftOf="@+id/CheckBox04"
          android:ems="10"
          android:inputType="text|date"
          android:onClick="showDatePickerDialog" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;EditText
          android:id="@+id/EditTextST"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/editTextSD"
          android:layout_alignBottom="@+id/editTextSD"
          android:layout_alignLeft="@+id/CheckBox04"
          android:ems="10"
          android:inputType="time"
          android:onClick="showTimePickerDialog" /&gt;
  
      &lt;TextView
          android:id="@+id/TextView02"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/editTextSD"
          android:layout_below="@+id/editTextSD"
          android:layout_marginTop="17dp"
          android:text="@string/select_end_date"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;EditText
          android:id="@+id/EditText01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/TextView02"
          android:layout_alignRight="@+id/editTextSD"
          android:layout_below="@+id/TextView02"
          android:ems="10"
          android:inputType="text|date"
          android:onClick="showDatePickerDialog" /&gt;
  
      &lt;EditText
          android:id="@+id/EditText02"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/EditText01"
          android:layout_alignBottom="@+id/EditText01"
          android:layout_alignLeft="@+id/EditTextST"
          android:ems="10"
          android:inputType="time"
          android:onClick="showTimePickerDialog" /&gt;
  
      &lt;TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/EditText01"
          android:layout_below="@+id/EditText01"
          android:layout_marginTop="17dp"
          android:text="@string/reminder"
          android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;Spinner
          android:id="@+id/spinner1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/textView3"
          android:entries="@array/reminder_times"
          android:paddingLeft="-10dp"
          android:gravity="left"
          tools:listitem="@android:layout/activity_list_item" /&gt;
  
      &lt;Button
          android:id="@+id/button1"
          style="?android:attr/buttonStyleSmall"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/textView3"
          android:layout_below="@+id/spinner1"
          android:layout_marginTop="119dp"
          android:text="@string/add_reminder" /&gt;
  
  
  
  &lt;/RelativeLayout&gt;
  
  &lt;/ScrollView&gt;
  </code></pre>