So I have a tablelayout and I am using weights to split the rows vertically on the screen. I am dynamically adding textviews into the third row, but as I am doing so, the more of them I add, the more the scrollview and tablerow expands vertically (even though I constrained it using weight and constraintTop...)
Screenshots:
ui with no textview: https://i.stack.imgur.com/MFfHb.jpg
ui with some textviews: https://i.stack.imgur.com/MQyaE.jpg
ui with a whole bunch of textviews: https://i.stack.imgur.com/agPLi.jpg (the first two tablerows become completely covered by the third row)
My goal is to have it like this: https://i.stack.imgur.com/8UPti.jpg
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
android:weightSum="1">
<TableRow
android:id="@+id/UIDcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/UID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</TableRow>
<TableRow
android:id="@+id/HOWTOcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/HOWTO"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Approach your phone to the terminal"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/SCROLLVIEWcontainer"
android:layout_weight=".6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="true">
<ScrollView
android:id="@+id/SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/SCROLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</ScrollView>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the code for dynamically adding textviews:
val scroll = findViewById<View>(R.id.SCROLL) as LinearLayout
val tv = TextView(this)
tv.text = "This is a textview" //my own function would go here
tv.setPaddingRelative(0, 0, 0, 16)
tv.gravity = 1
scroll.addView(tv)
The weight is the
importancewhen assigning spaceFrom the Docs
You have give the scrollview more weight thus as you add more items it takes more space.
It look like you are actually trying to assign percentages of the screen
so something like the following should meet you needs and will perform better and be more flexible
Producing:-