title: Dynamically changing UI using merge and include tags depending on click
body: <p>I'm new to android and programming in general so go easy!</p>
  
  <p>I have created a - probably overcomplicated but it does what I want - main.xml with my layout which goes something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_scrollview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      &gt;
  &lt;LinearLayout
      ...
      &gt;
      &lt;TextView  
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/title"
          android:gravity="center"
      /&gt;
      &lt;TableLayout
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:stretchColumns="0"
          &gt;
          &lt;TableRow&gt;
              &lt;TextView/&gt;
              &lt;Spinner/&gt;
          &lt;/TableRow&gt;
          &lt;TableRow&gt;
              &lt;TextView/&gt;
              &lt;Spinner/&gt;
          &lt;/TableRow&gt;
      &lt;/TableLayout&gt;
      &lt;TableLayout
          android:id="@+id/main_tablelayout2"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:stretchColumns="*"
          &gt;
          &lt;include
              android:id="@+id/include"
              layout="@layout/price0"
          /&gt;
          &lt;TableRow&gt;
              &lt;TextView/&gt;
              &lt;EditText/&gt;
          &lt;/TableRow&gt;
      &lt;/TableLayout&gt;
  &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>This is one of the options that I'm trying to include (price1.xml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;merge xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  &lt;TableRow&gt;
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:text="@string/price1"
          android:gravity="center_vertical"
      /&gt;
      &lt;EditText
          android:id="@+id/price1"
      /&gt;
  &lt;/TableRow&gt; 
  &lt;/merge&gt;
  </code></pre>
  
  <p>I am trying to dynamically change the include depending on what value one of the spinners has, but I get a force close on starting the app.</p>
  
  <p>The logcat error is:</p>
  
  <pre><code>01-18 15:31:42.826: ERROR/AndroidRuntime(719): FATAL EXCEPTION: main
  01-18 15:31:42.826: ERROR/AndroidRuntime(719): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:421)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:403)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.TableLayout.addView(TableLayout.java:394)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.kavi.eta.Calculate$1.onItemSelected(Calculate.java:36)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView.access$200(AdapterView.java:42)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Handler.handleCallback(Handler.java:587)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Handler.dispatchMessage(Handler.java:92)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.os.Looper.loop(Looper.java:123)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at android.app.ActivityThread.main(ActivityThread.java:3647)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at java.lang.reflect.Method.invoke(Method.java:507)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  01-18 15:31:42.826: ERROR/AndroidRuntime(719):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Looks like I'm doing the java code wrong but I can't figure out how to properly remove the default include and replace it with another layout:</p>
  
  <pre><code>public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int position, long id) { 
              int index = formatSpinner.getSelectedItemPosition();
              if (index == 0) {
  
                  TableLayout tl = (TableLayout)findViewById(R.id.main_tablelayout2);
                  tl.removeView(findViewById(R.id.include));
  
                  LayoutInflater inflater = getLayoutInflater();
                  tl.addView(inflater.inflate(R.layout.price1, tl, true));
              }
  </code></pre>
  
  <p>Sorry about the long post but I've searched everywhere and can't find a clear answer. Hopefully this'll at least provide a decent-ish example...</p>