I use a table layout to display data as table, but I want a table with user-defined columns and rows with borders. Suggestions?
I use a table layout to display data as table, but I want a table with user-defined columns and rows with borders. Suggestions?
Well that may inspire u Those steps show how to create bordered table dynamically
here is the table view
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simpleTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:stretchColumns="*"
>
</TableLayout>
</android.support.v4.widget.NestedScrollView>
and here the row to use "attrib_row.xml"
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/border"
>
<TextView
android:id="@+id/attrib_name"
android:textStyle="bold"
android:height="30dp"
android:background="@drawable/border"
android:gravity="center"
/>
<TextView
android:id="@+id/attrib_value"
android:gravity="center"
android:height="30dp"
android:textStyle="bold"
android:background="@drawable/border"
/>
</TableRow>
and we can add this xml file to drawable to add border to our table "border.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="@color/colorAccent"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>
and finally here is the compact code written in Kotlin but it's easy to convert it to java if you need
well temps is an array list contain data: ArrayList<Double>()
fun CreateTable()
{
val temps=controller?.getTemps()
val rowHead = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
(rowHead.findViewById<View>(R.id.attrib_name) as TextView).text=("time")
(rowHead.findViewById<View>(R.id.attrib_value) as TextView).text=("Value")
table!!.addView(rowHead)
for (i in 0 until temps!!.size) {
val row = LayoutInflater.from(context).inflate(R.layout.attrib_row, null) as TableRow
(row.findViewById<View>(R.id.attrib_name) as TextView).text=((i+1).toString())
(row.findViewById<View>(R.id.attrib_value) as TextView).text=(temps[i].toString())
table!!.addView(row)
}
table!!.requestLayout()
}
and you can use it in your fragment for example like this
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
table = view?.findViewById<View>(R.id.simpleTableLayout) as TableLayout
CreateTable()
}
If you need table with the border, I suggest linear layout with weight instead of TableLayout.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:padding="7dp"
android:background="@drawable/border"
android:textColor="@android:color/white"
android:text="PRODUCT"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/black"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:paddingBottom="1dp"
android:baselineAligned="false">
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp">
<TextView
android:id="@+id/chainprod"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/pdct"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:id="@+id/chainthick"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/thcns"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:id="@+id/chainsize"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/size" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/sqft" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/black"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:paddingBottom="1dp"
android:baselineAligned="false">
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp">
<TextView
android:id="@+id/viewchainprod"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/pdct" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:id="@+id/viewchainthick"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/thcns"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:id="@+id/viewchainsize"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/size"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_marginStart="1dp">
<TextView
android:id="@+id/viewchainsqft"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@android:color/white"
android:gravity="center"
android:textColor="@android:color/black"
android:text="@string/sqft"/>
</LinearLayout>
</LinearLayout>