Two identical TableLayout's identical TableRow's not showing equal android

31 Views Asked by At

I created two identical TableLayout's in my xml file

And programmatically added two Identical(everything is same except variable names) TableRows with TextView's but they're not showing the same in app.

Here's the xml and the image:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/detected_drones"
        android:textSize = "25sp"
        android:textStyle="bold"
        android:textColor="@android:color/black">
    </TextView>

    //FIRST TABLELAYOUT
    <TableLayout
        android:id="@+id/zaberlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1,2">
    </TableLayout>

    //SECOND TABLELAYOUT
    <TableLayout
        android:id="@+id/naberlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/zaberlist"//ONLY DIFFERENCE BETWEEN THEM
        android:stretchColumns="0,1,2">
    </TableLayout>

    <Button
        android:id="@+id/scanBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:visibility="visible"
        android:background="@android:color/holo_green_dark"
        android:text="@string/start_scan"
        android:layout_alignParentBottom="true"/>

    <Button
        android:id="@+id/scanBtnStop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:visibility="gone"
        android:background="@android:color/holo_red_dark"
        android:baselineAligned="false"
        android:text="@string/stop_scan"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

IMAGE

edit: here's how I add those rows to the table:


                val tr_head1 = TableRow(this@WIFIScannerActivity)
                tr_head1.layoutParams = TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT
                )

                val label_name1 = TextView(this@WIFIScannerActivity)
                label_name1.text = scanResult.SSID
                if(scanResult.SSID==""){
                    label_name1.text = "*hidden*"
                }
                label_name1.textSize = 14F
                label_name1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
                label_name1.setPadding(20,0,0,0)
                label_name1.setTypeface(null, Typeface.BOLD)
                tr_head1.addView(label_name1) // add the column to the table row here

                val label_mac1 = TextView(this@WIFIScannerActivity) // part3
                label_mac1.text = scanResult.BSSID
                label_mac1.textSize = 14F
                label_mac1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
                label_mac1.setTypeface(null, Typeface.BOLD)
                tr_head1.addView(label_mac1) // add the column to the table row here

                val label_distance1 = TextView(this@WIFIScannerActivity) // part3
                label_distance1.text = String.format("%.2f", Math.pow(10.0, (((-20 - scanResult.level).toDouble())/20)))//Math.pow(10.0, ((-20 - scanResult.level.toDouble()) / 20)).toString()
                label_distance1.textSize = 14F
                label_distance1.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1F)
                label_distance1.setPadding(0,0,20,0)
                label_distance1.setTypeface(null, Typeface.BOLD)
                tr_head1.addView(label_distance1)



                tr_head1.setBackgroundColor(Color.LTGRAY)
                tr_head1.setPadding(0,10,0,10)

                //I only change xdlist here to add to different tables
                xdlist!!.addView(tr_head1, TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT
                ))

1

There are 1 best solutions below

2
Andrew On

If the data in the two tables is identical (including spaces) then in theory the results should be the same for the two tables BUT in real usage it's unlikely that you would be displaying the identical data in two tables next to each other.

So if you want to align column's correctly just use one TableLayout and multiple TableRow items, because a TableLayout is only designed to align within each Table and not between tables.

Alternative use a different type of Layout, probably better to be one based on a RecyclerView, or a ConstraintLayout, but with understanding all the type of data and how you want the data to be presented I cannot advise on the best method.