Search code examples
androidandroid-layoutandroid-scrollviewandroid-tablelayout

Nesting a ScrollView inside a TableRow


My intention is to make my TextView scrollable by nesting it inside a ScrollView (although I know that TextView can be made scrollable on its own (without needing a parent ScrollView), I wanted to do it in this way :) ) and have the ScrollView fill the entire width of the screen.

The layout in the following code works, but the ScrollView does not fill the entire width of the screen. However, its parent TableRow fills the entire width.

I have tried substituting the values of android:layout_width of ScrollView with fill_parent, match_parent and wrap_content but none of them are filling the width completely.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_my">

    <TableRow>
        <EditText android:id="@+id/edit_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/edit_message"
            android:layout_weight="1"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/button_send"
            android:onClick="sendMessage"/>
    </TableRow>

    <TableRow>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:fillViewport="true">
            <TextView
                android:id="@+id/message_box"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </ScrollView>
    </TableRow>

</TableLayout>

Solution

  • I found a solution! I added android:layout_weight="1" to ScrollView.

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_weight="1"
            android:fillViewport="true">
            <TextView
                android:id="@+id/message_box"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
    </ScrollView>