title: How to create a custom table like structure using Grid Layout in Android Studio?
body: <p>I want to create a custom report which should look something like this in Android:
  <a href="https://i.stack.imgur.com/QTseV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QTseV.png" alt="enter image description here" /></a></p>
  <p>The number of schools and number of years will be dynamic. I had created this using the Table Layout but since it is very large data it is taking way too much long to display results.
  I have also tried using Grid Layout but the column spanning is not working there, the result is just coming as all columns with same width. If I try to set the width using GridLayout.params, it only accepts size in pixels so it is visible differently on various devices.</p>
  <p>Is there any way i can specify column width in dp or span the column?
  <code>titleTxtSpecColumn= GridLayout.spec (5,5)</code> is not working, its still displaying as wrap content.</p>
  <pre><code>       val gridLayout = findViewById&lt;GridLayout&gt;(R.id.reportgrid)
         var titleRowSpec: GridLayout.Spec
         var titleTxtSpecColumn: GridLayout.Spec
          titleRowSpec= GridLayout.spec(1)
          titleTxtSpecColumn= GridLayout.spec (0,4)
          val tv0 = layoutInflater.inflate(R.layout.gridcell_header, gridLayout, false) as TextView?
          tv0!!.text = getString(R.string.lbl_date)
          tv0.setBackgroundResource(R.drawable.tablerow_border)
          gridLayout.addView(tv0, GridLayout.LayoutParams(titleRowSpec, titleTxtSpecColumn))
  
          titleTxtSpecColumn= GridLayout.spec (4)
          val tv1 = layoutInflater.inflate(R.layout.gridcell_header, gridLayout, false) as TextView?
          tv1!!.text = getString(R.string.lbl_grade)
          tv1.setBackgroundResource(R.drawable.tablerow_border)
          gridLayout.addView(tv1, GridLayout.LayoutParams(titleRowSpec, titleTxtSpecColumn))
  
          titleTxtSpecColumn= GridLayout.spec (5,5)
          val tv2 = layoutInflater.inflate(R.layout.gridcell_header, gridLayout, false) as TextView?
          tv2!!.text = getString(R.string.lbl_grade)
          tv2.setBackgroundResource(R.drawable.tablerow_border)
          gridLayout.addView(tv2, GridLayout.LayoutParams(titleRowSpec, titleTxtSpecColumn))
  </code></pre>
  <p>Layout:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.activities.ReportActivity&quot;
      android:background=&quot;@color/eggshell&quot;&gt;
  
      &lt;androidx.appcompat.widget.Toolbar
          android:id=&quot;@+id/toolbar&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          style=&quot;@style/Theme.Account.Toolbar&quot;&gt;
          &lt;TextView
              android:id=&quot;@+id/custom_title&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;@string/app_name&quot;
              style=&quot;@style/Toolbar&quot;
              /&gt;
      &lt;/androidx.appcompat.widget.Toolbar&gt;
  
      &lt;ScrollView
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:scrollbars=&quot;vertical&quot;
          android:scrollbarStyle=&quot;outsideInset&quot;
          android:fillViewport=&quot;true&quot;&gt;
          &lt;HorizontalScrollView
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:scrollbars=&quot;horizontal&quot;&gt;
              &lt;GridLayout
                  android:id=&quot;@+id/reportgrid&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:background=&quot;@drawable/tablerowdata_border&quot;
                  tools:ignore=&quot;ScrollViewSize&quot;&gt;
                  
              &lt;/GridLayout&gt;
          &lt;/HorizontalScrollView&gt;
      &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;TextView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;350dp&quot;
      android:layout_height=&quot;0dp&quot;
      style=&quot;@style/ReportHeading&quot;/&gt;
  </code></pre>
  <p>Style:</p>
  <pre><code>&lt;style name=&quot;ReportHeading&quot; parent=&quot;@style/Widget.MaterialComponents.TextView&quot;&gt;
      &lt;item name=&quot;android:textColor&quot;&gt;@color/black&lt;/item&gt;
      &lt;item name=&quot;android:textSize&quot;&gt;18sp&lt;/item&gt;
      &lt;item name=&quot;android:textAlignment&quot;&gt;center&lt;/item&gt;
      &lt;item name=&quot;android:textStyle&quot;&gt;bold&lt;/item&gt;
      &lt;item name=&quot;android:padding&quot;&gt;8dp&lt;/item&gt;
      &lt;item name=&quot;android:background&quot;&gt;@color/colorLightGray&lt;/item&gt;
  </code></pre>
  <p>Border:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
      &lt;solid android:color=&quot;@color/colorLightGray&quot; /&gt;
      &lt;stroke android:width=&quot;2dp&quot; android:color=&quot;@color/black&quot; /&gt;
  &lt;/shape&gt;
  </code></pre>