Layout weight for text view inside a scrollview

65 Views Asked by At

I have a layout with two TextInputLayout with layout weights set for their heights and a table layout like this

Layout

And here is the layout file

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:hint="@string/code_hint"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="top"
            android:scrollbars="vertical"
            android:id="@+id/code_field"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:hint="@string/input_hint"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="top"
            android:id="@+id/input_field"/>

    </com.google.android.material.textfield.TextInputLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="50dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp">

        <TableRow
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center">

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/run_button"
                android:text="@string/run"
                app:icon="@drawable/ic_baseline_play_arrow_24"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            style="?android:attr/buttonBarStyle">

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/plus_button"
                android:text="@string/plus"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/minus_button"
                android:text="@string/minus"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/left_shift_button"
                android:text="@string/left_shift"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/right_shift_button"
                android:text="@string/right_shift"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/backspace_button"
                app:icon="@drawable/ic_baseline_backspace_24"
                style="?android:attr/buttonBarButtonStyle"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            style="?android:attr/buttonBarStyle">

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/start_loop_button"
                android:text="@string/start_loop"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/end_loop_button"
                android:text="@string/end_loop"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/dot_button"
                android:text="@string/dot"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

            <com.google.android.material.button.MaterialButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:id="@+id/comma_button"
                android:text="@string/comma"
                android:textSize="20sp"
                style="?android:attr/buttonBarButtonStyle"/>

        </TableRow>

    </TableLayout>

</LinearLayout>

</ScrollView>

The problem I have is, when I have my phone in portrait mode and I enter text into the fields that is bigger than what can fit into the field, the fields remain the same size and start to scroll. But, when I rotate the phone, the text fields grow in size to fit the entire text written into the text field and stay at that size. I do not want this to happen. Seems like the layout weights are ignored when I rotate the device.

How do I solve this?

0

There are 0 best solutions below