androidandroid-layoutuser-interfaceandroid-scrollviewandroid-tablelayout

Scrollview keeps expanding vertically the more textviews are added into it


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.sstatic.net/MFfHb.jpg

ui with some textviews: https://i.sstatic.net/MQyaE.jpg

ui with a whole bunch of textviews: https://i.sstatic.net/agPLi.jpg (the first two tablerows become completely covered by the third row)

My goal is to have it like this: https://i.sstatic.net/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)

Solution

  • The weight is the importance when assigning space

    From the Docs

    This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

    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

    <?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">
    
    
    <TextView
       android:id="@+id/UID"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:gravity="center"
       android:text="TextView"
       app:layout_constraintHeight_percent="0.2"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
       android:id="@+id/HOWTO"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:gravity="center"
       android:text="Approach your phone to the terminal"
       android:textStyle="bold"
       app:layout_constraintHeight_percent="0.2"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/UID"/>
    
    <ScrollView
       android:id="@+id/SCROLLVIEW"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       app:layout_constraintHeight_percent="0.6"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/HOWTO"
       tools:ignore="SpeakableTextPresentCheck">
    
       <LinearLayout
           android:id="@+id/SCROLL"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical"/>
    </ScrollView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    

    Producing:-

    Sample