title: Android Adding Dynamic ListView Header
body: <p>I have a list view and I am trying to set header for it so that it looks like a table header. Here is the XML file of my list view row:</p>
  
  <p><strong>PS: I have removed some codes to shorten the question</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;TextView
      android:id="@+id/txtDisplayDate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1" /&gt;
  &lt;TextView
      android:id="@+id/txtDisplayCat"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And my XML file of my header list view:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;TextView
      android:id="@+id/txtDisplayDate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Date" /&gt;
  &lt;TextView
      android:id="@+id/txtDisplayCat"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Category" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>And the class where I populate the list view:</p>
  
  <pre><code>private class ListAdapter extends BaseAdapter {
      LayoutInflater inflater;
      ViewHolder viewHolder;
  
      public ListAdapter(Context context) {
          // TODO Auto-generated constructor stub
          inflater = LayoutInflater.from(context);
          LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
                  R.layout.trans_header_layout, null);
          listview.addHeaderView(listHeaderView);
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return _translist.size();
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {     
          // TODO Auto-generated method stub
          if (convertView == null) {
  
              convertView = inflater.inflate(R.layout.trans_listview_row,
                      null);
              viewHolder = new ViewHolder();
  
              viewHolder.txt_ddate = (TextView) convertView
                      .findViewById(R.id.txtDisplayDate);
              viewHolder.txt_dcat = (TextView) convertView
                      .findViewById(R.id.txtDisplayCat);
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          viewHolder.txt_ddate.setText(_translist.get(position).getDate()
                  .trim());
          viewHolder.txt_dcat.setText(_translist.get(position)
                  .getCategory().trim());
          return convertView;
      }
  }
  </code></pre>
  
  <p>However, by using these codes, here is the output that I've gotten:</p>
  
  <p><img src="https://i.stack.imgur.com/2knCu.png" alt="enter image description here"></p>
  
  <p>The header does not match with the column below it. I wonder is there any alternate way to add header to list view so that it looks like a table header.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My transaction_rec.XML where the listview is located:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" &gt;
  
      &lt;LinearLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:orientation="vertical" &gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Date" /&gt;
  
          &lt;TextView
              android:id="@+id/txtDisplayDate"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:orientation="vertical" &gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Category" /&gt;
  
          &lt;TextView
              android:id="@+id/txtDisplayCat"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" /&gt;
      &lt;/LinearLayout&gt;  
  &lt;/LinearLayout&gt;
  
  &lt;ListView
      android:id="@+id/listview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And my transactionRec.java:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.transaction_rec);
      context = this;
      listview = (ListView) findViewById(R.id.listview);
  
      //Code to get data from database
  }
  
  private class ListAdapter extends BaseAdapter {
      LayoutInflater inflater;
      ViewHolder viewHolder;
  
      public ListAdapter(Context context) {
          // TODO Auto-generated constructor stub
          inflater = LayoutInflater.from(context);
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return _translist.size();
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {     
          // TODO Auto-generated method stub
          if (convertView == null) {
  
              convertView = inflater.inflate(R.layout.transaction_rec,
                      null);
              viewHolder = new ViewHolder();
  
              viewHolder.txt_ddate = (TextView) convertView
                      .findViewById(R.id.txtDisplayDate);
  
              viewHolder.txt_dcat = (TextView) convertView
                      .findViewById(R.id.txtDisplayCat);
  
          } else {
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          viewHolder.txt_ddate.setText(_translist.get(position).getDate()
                  .trim());
          viewHolder.txt_dcat.setText(_translist.get(position)
                  .getCategory().trim());
  
          return convertView;
          }
      }
  }
  </code></pre>
  
  <p>And the output I've gotten now:</p>
  
  <p><img src="https://i.stack.imgur.com/qYEte.png" alt="enter image description here"></p>