title: Scrollable rows with fixed static header using TableLayout in android
body: <p>I am working on an android application where I want to display the data into TableLayout from database. In this TableLayout, the column headers are fixed in vertical scrolling but should be scrollable in horizontal scrolling.</p>
  
  <p>I have done this so far.</p>
  
  <p>table.xml</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"&gt;
  
  &lt;HorizontalScrollView
      android:id="@+id/horizontalView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dip"
      android:scrollbars="horizontal|vertical" &gt;
  
      &lt;LinearLayout
          android:id="@+id/linearLayout2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:gravity="center_horizontal"
          android:orientation="vertical" &gt;
  
          &lt;TableLayout
              android:id="@+id/tableLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:divider="@color/white"
              android:showDividers="middle"
              android:stretchColumns="*" &gt;
          &lt;/TableLayout&gt;
  
          &lt;ScrollView
              android:id="@+id/layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="5dip"
              android:fillViewport="true"
              android:scrollbarStyle="outsideInset"
              android:scrollbars="horizontal|vertical" &gt;
  
              &lt;TableLayout
                  android:id="@+id/tableLayout1"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:divider="@color/white"
                  android:showDividers="middle"
                 android:stretchColumns="*" &gt;
              &lt;/TableLayout&gt;
          &lt;/ScrollView&gt;
      &lt;/LinearLayout&gt;
  &lt;/HorizontalScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Activity table.java</p>
  
  <pre><code>private void BuildTable() {
  
      DbHelper = new DBhelper(this);
      DbHelper.open();
      Cursor c = DbHelper.getDetails();
  
      int rows = c.getCount();
      int cols = c.getColumnCount();
  
      // Headers
      row = new TableRow(this);
      row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
              LayoutParams.WRAP_CONTENT));
      TextView serialNo = new TextView(this);
      serialNo.setBackgroundResource(R.drawable.valuecellborder);
      serialNo.setTextColor(Color.parseColor("#FFFFFF"));
      serialNo.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      serialNo.setGravity(Gravity.CENTER);
      serialNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      serialNo.setTextSize(20);
      serialNo.setPadding(15, 15, 15, 20);
      serialNo.setText("S.No.");
      row.addView(serialNo);
  
      TextView custName = new TextView(this);
      custName.setBackgroundResource(R.drawable.valuecellborder);
      custName.setTextColor(Color.parseColor("#FFFFFF"));
      custName.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      custName.setGravity(Gravity.CENTER);
      custName.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      custName.setTextSize(20);
      custName.setPadding(15, 15, 15, 20);
      custName.setText("Customer");
      row.addView(custName);
  
  
      TextView Address = new TextView(this);
      Address.setBackgroundResource(R.drawable.valuecellborder);
      Address.setTextColor(Color.parseColor("#FFFFFF"));
      Address.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      Address.setGravity(Gravity.CENTER);
      Address.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      Address.setTextSize(20);
      Address.setPadding(15, 15, 15, 20);
      Address.setText("Address");
      row.addView(Address);
  
  
      TextView FatherName = new TextView(this);
      FatherName.setBackgroundResource(R.drawable.valuecellborder);
      FatherName.setTextColor(Color.parseColor("#FFFFFF"));
      FatherName.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      FatherName.setGravity(Gravity.CENTER);
      FatherName.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      FatherName.setTextSize(20);
      FatherName.setPadding(15, 15, 15, 20);
      FatherName.setText("Fathers Name");
      row.addView(FatherName);
  
      TextView openingDate = new TextView(this);
      openingDate.setBackgroundResource(R.drawable.valuecellborder);
      openingDate.setTextColor(Color.parseColor("#FFFFFF"));
      openingDate.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      openingDate.setGravity(Gravity.CENTER);
      openingDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      openingDate.setTextSize(20);
      openingDate.setPadding(15, 15, 15, 20);
      openingDate.setText("Opening Date");
      row.addView(openingDate);
  
      TextView interestRate = new TextView(this);
      interestRate.setBackgroundResource(R.drawable.valuecellborder);
      interestRate.setTextColor(Color.parseColor("#FFFFFF"));
      interestRate.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      interestRate.setGravity(Gravity.CENTER);
      interestRate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
      interestRate.setTextSize(20);
      interestRate.setPadding(15, 15, 15, 20);
      interestRate.setText("Interest Rate");
      row.addView(interestRate);
      table_layout.addView(row);
  
      if (rows == 0) {
          Common.showToast(TableActivity.this, "Data Not Found !",
                  "long");
      } else {
  
          for (int i = 0; i &lt; rows; i++) {
  
              row = new TableRow(this);
               row.setLayoutParams(new
               LayoutParams(LayoutParams.MATCH_PARENT,
               LayoutParams.WRAP_CONTENT));
  
              // inner for loop
              for (int j = 0; j &lt; cols; j++) {
  
                  TextView tv = new TextView(this);
                  tv.setBackgroundResource(R.drawable.valuecellborder);
                  tv.setTextColor(Color.parseColor("#FFFFFF"));
                  tv.setLayoutParams(new TableRow.LayoutParams(
                          LayoutParams.WRAP_CONTENT,
                          LayoutParams.WRAP_CONTENT));
                  tv.setGravity(Gravity.CENTER);
                  tv.setTextSize(18);
                  tv.setPadding(15, 15, 15, 15);
                  tv.setText(c.getString(j));
                  row.addView(tv);
  
              }
  
              c.moveToNext();
  
              tablelayout.addView(row);
  
          }
  
      }
      DbHelper.close();
  }
  </code></pre>
  
  <p>valuecellborder.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" &gt;
  &lt;stroke
      android:width="0.1dp"
      android:color="#ffffff" /&gt;
  
  &lt;padding
      android:bottom="0dp"
      android:left="0dp"
      android:right="0dp"
      android:top="0dp"/&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>Functionally, its perfect. The only problem is the border. The vertical borders of headers and its data does not match perfectly.
  Here's the screenshot.</p>
  
  <p><a href="https://i.stack.imgur.com/Gfy5G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gfy5G.png" alt="enter image description here"></a></p>
  
  <p>I have tried but not got the desired results.
  How can i resolve this?
  Please help me.
  Thank you.</p>