Search code examples
androidandroid-layoutlayout

How to make a scrollable layout been just followed by another layout in an Android activity


Here is what I want to get in my Android activity:

+------------------++------------------++------------------+
| +--------------+ || +--------------+ || +--------------+ |
| |    Table     | || |    Table     | || |    Table    #| |
| |              | || |              | || |             #| |
| |              | || |              | || |             #| |
| |              | || |              | || |             #| |
| +--------------+ || |              | || |             #| |
| +--------------+ || |              | || |             #| |
| | Linearlayout | || |              | || |             #| |
| +--------------+ || +--------------+ || |              | |
|                  || +--------------+ || |              | |
|                  || | Linearlayout | || +--------------+ |
|                  || +--------------+ || +--------------+ |
|                  ||                  || | Linearlayout | |
|                  ||                  || +--------------+ |
|  +------------+  ||  +------------+  ||  +------------+  |
|  |   Button   |  ||  |   Button   |  ||  |   Button   |  |
|  +------------+  ||  +------------+  ||  +------------+  |
+------------------++------------------++------------------+

Goals:

  • I have a table of variable length.
  • The table shall be scrollable if it cannot be fully displayed
  • I want the Linearlayout to always be next to the table
  • The button shall always be positioned at the bottom

I have tried many combinations of ConstraintLayout and Linearlayout without success:

  • Or the Linearlayout disappears when the table is too long
  • Or the Linearlayout stays close to the Button.

Here is an example of code (simplified) that does not work:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent"
                  app:layout_constraintBottom_toTopOf="@id/B2"
                  android:orientation="vertical">
        <ScrollView android:id="@+id/S1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:layout_weight="0">
            <TableLayout android:id="@+id/table"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content">
                <TableRow >...</TableRow>
                <TableRow >...</TableRow>
                <TableRow >...</TableRow>
            </TableLayout>
        </ScrollView>
        <LinearLayout>.....</LinearLayout>
    </LinearLayout>
    <Button android:id="@+id/B2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="validate"
            app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I know I can achieve this by using an OnPreDrawListener, but I would prefer to have something more static, just with layouts in XML.

How can I achieve this?


Solution

  • this should work, atleast in the preview it did: I hope I understood your requirements correctly.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_max="wrap"
            app:layout_constraintVertical_bias="0"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@id/button">
    
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/green_like">
    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="100dp"/>
    
                </TableRow>
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/red_400">
    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="50dp"/>
                </TableRow>
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/green_like">
    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="50dp"/>
    
                </TableRow>
            </TableLayout>
    
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/primary"
            android:orientation="vertical"
            app:layout_constraintVertical_bias="0"
            app:layout_constraintTop_toBottomOf="@id/scrollView"
            app:layout_constraintBottom_toTopOf="@id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="validate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    LinearLayout is blue, table cellsgreen and red

    small table

    big table