title: Android Fragment table display
body: <p>I have an app in which I use fragments to display a dynamically generated table in the right pane.
  This works if I comment out the code for the table so that ststic code in the layout file is displayed in the right pane, but when I include the table it inserts itself into the left hand panel  - it should be under the text in the right pane.
  Extracts attached from layout file and code for inserting the table (cannot upload screen shots as new user).
  I've tried numerous solutions and assume there is something wrong with the table code, either in the layout or in the Java.</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="50dip"
    android:background="@color/background_color"
    &gt;
  
  &lt;TextView 
      android:id="@+id/display_readings_title"
      style="@style/textstyle.Heading"
      android:layout_below="@+id/logout" 
      android:text="@string/display_readings_title" 
      android:layout_gravity="center"
      android:layout_marginTop="30dip"
      android:layout_marginBottom="10dip"
      android:layout_centerHorizontal="true"
      /&gt;
  
  &lt;TextView 
      android:id="@+id/patient_id_text"
      style="@style/textstyle"
      android:text="@string/patient_id_text" 
      android:layout_marginTop="20dip"
      android:layout_marginLeft="10dip"
      android:layout_below="@id/display_readings_title"
      /&gt;
  
      &lt;TextView 
      android:id="@+id/patient_id"
      style="@style/textstyle"
      android:text="" 
      android:layout_marginLeft="10dip"
      android:layout_below="@id/display_readings_title"
      android:layout_toRightOf="@id/patient_id_text"
      android:layout_alignTop="@id/patient_id_text"
          /&gt;
  
       &lt;TextView 
      android:id="@+id/patient_name_text"
      style="@style/textstyle"
      android:text="@string/patient_name_text" 
      android:layout_marginTop="5dip"
      android:layout_marginLeft="10dip"
      android:layout_below="@id/patient_id_text"
      /&gt;
  
      &lt;TextView 
      android:id="@+id/patient_name"
      style="@style/textstyle"
      android:text="" 
      android:layout_marginLeft="10dip"
      android:layout_below="@id/patient_id_text"
      android:layout_toRightOf="@id/patient_name_text"
      android:layout_alignTop="@id/patient_name_text"
          /&gt;
  
  &lt;ScrollView android:id="@+id/ScrollPatientReadings"
      android:layout_width="wrap_content" 
      android:fillViewport="true" 
      android:layout_height="wrap_content"
      android:layout_below="@+id/patient_name_text"
      android:layout_marginTop="20dip"
  
      &gt;   
  
  &lt;TableLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:stretchColumns="0,1,2,3" 
      android:id="@+id/patient_table"
      android:layout_marginLeft="10dip"
      &gt;
  &lt;/TableLayout&gt;
  
  &lt;/ScrollView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Extract from Java code</p>
  
  <pre><code>patient_table = (TableLayout) getActivity().findViewById(
                  R.id.patient_table);
  
          // get patient's readings from the database
  
                  db.open();
                  createTableTitle();
                  Cursor c = db.getPatientReadings(idtext);
                  extractValues(c);
                  db.close();
              }
  
      // create title row
  
          private void createTableTitle() {
              TableRow titlerow;
              TextView tl1, tl2, tl3, tl4;
              titlerow = new TableRow(getActivity());
              titlerow.setPadding(10, 10, 0, 10);
  
              tl1 = new TextView(getActivity());
              tl1.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              tl1.setTextSize(20);
              tl2 = new TextView(getActivity());
              tl2.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              tl2.setTextSize(20);
  
              tl3 = new TextView(getActivity());
              tl3.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              tl3.setTextSize(20);
  
              tl4 = new TextView(getActivity());
              tl4.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              tl4.setTextSize(20);
  
              tl1.setText("INR");
              tl2.setText("Surface Bleeding");
              tl3.setText("Internal Bleeding");
              tl4.setText("Date");
  
              titlerow.addView(tl1);
              titlerow.addView(tl2);
              titlerow.addView(tl3);
              titlerow.addView(tl4);
  
              patient_table.addView(titlerow);
  
          }
  
          private void extractValues(Cursor c) {
              if (c.moveToFirst()) {
                  do {
                      String inrReading = c.getString(2);
                      String surface_bleeds = c.getString(3);
                      String internal_bleeding = c.getString(4);
                      String currentDateTimeString = c.getString(5);
  
                      fillPatientTable(inrReading, surface_bleeds, internal_bleeding,
                              currentDateTimeString);
                  } while (c.moveToNext());
              }
          }
  
          // Display in table
  
          private void fillPatientTable(String inrReading, String surface_bleeds,
                  String internal_bleeding, String currentDateTimeString) {
  
              TableRow row;
              TextView t1, t2, t3, t4;
              row = new TableRow(getActivity());
              row.setPadding(10, 0, 0, 0);
  
              t1 = new TextView(getActivity());
              t1.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              t1.setTextSize(18);
              t2 = new TextView(getActivity());
              t2.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              t2.setTextSize(18);
              t3 = new TextView(getActivity());
              t3.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              t3.setTextSize(18);
              t4 = new TextView(getActivity());
              t4.setTextColor(getActivity().getResources().getColor(R.color.text_color));
              t4.setTextSize(18);
  
              t1.setText(inrReading);
              t2.setText(surface_bleeds);
              t3.setText(internal_bleeding);
              t4.setText(currentDateTimeString);
  
              row.addView(t1);
              row.addView(t2);
              row.addView(t3);
              row.addView(t4);
  
              patient_table.addView(row);    
  </code></pre>