Skip to content
Advertisement

Android Constraint Layout in Scroll View not showing the last item

I’m trying to make a fragment that have a sort of form with lots of textViews, EditTexts and spinners, so i have to use a scrollView to see all the items.

For some reason the last item is not showing and i don’t understand why, I read lots of questions about this problem but no one works for me.

Can someone help me to solve this problem?

The xml:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".UI.newTicket.NewTicketFragment">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/formLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="60dp">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/titleText"
                style="@style/TitleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:text="@string/new_ticket_text"
                android:textColor="@color/white"
                app:layout_constraintBottom_toBottomOf="@id/toolbar"
                app:layout_constraintStart_toStartOf="@id/toolbar"
                app:layout_constraintTop_toTopOf="@id/toolbar" />

            <TextView
                android:id="@+id/textView4"
                style="@style/TitleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="20dp"
                android:text="@string/title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/toolbar" />

            <EditText
                android:id="@+id/titleInput"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Titolo del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/insertTitle_text"
                android:inputType="text"
                android:maxLines="1"
                android:padding="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView4" />

            <TextView
                android:id="@+id/textView5"
                style="@style/TitleStyle"
                android:text="@string/description_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/titleInput" />

            <EditText
                android:id="@+id/descriptionInput"
                android:layout_width="350dp"
                android:layout_height="70dp"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Descrizione del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/InsertDescr_text"
                android:inputType="textMultiLine"
                android:padding="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView5" />

            <TextView
                android:id="@+id/TextView6"
                style="@style/TitleStyle"
                android:text="@string/customer_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/descriptionInput" />

            <com.toptoche.searchablespinnerlibrary.SearchableSpinner
                android:id="@+id/customerSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView6" />

            <TextView
                android:id="@+id/TextView7"
                style="@style/TitleStyle"
                android:text="@string/field_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/customerSpinner" />

            <Spinner
                android:id="@+id/fieldSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView7" />

            <TextView
                android:id="@+id/TextView8"
                style="@style/TitleStyle"
                android:text="@string/category_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fieldSpinner" />

            <Spinner
                android:id="@+id/categorySpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView8" />


            <TextView
                android:id="@+id/TextView9"
                style="@style/TitleStyle"
                android:text="@string/priority_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/categorySpinner" />

            <Spinner
                android:id="@+id/prioritySpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView9" />

            <TextView
                android:id="@+id/deadlineTitle"
                style="@style/TitleStyle"
                android:text="@string/deadline_text"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/prioritySpinner" />

            <EditText
                android:id="@+id/deadlineInput"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Scadenza del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/deadlineHint_text"
                android:inputType="date"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/deadlineTitle" />


            <TextView
                android:id="@+id/TextView11"
                style="@style/TitleStyle"
                android:text="@string/AssignTo_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/deadlineInput" />

            <Spinner
                android:id="@+id/assignmentSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView11" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/createButton"
                style="@style/createButton"
                android:layout_width="350dp"
                android:layout_height="60dp"
                android:layout_marginTop="10dp"
                android:text="@string/create_text"
                app:cornerRadius="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/assignmentSpinner"
                app:strokeColor="@color/light_blue"
                app:strokeWidth="2dp" />


            <!--  Used only for show the button
            <TextView
                android:id="@+id/uselessItem"
                android:layout_width="1dp"
                android:layout_height="50dp"
                android:visibility="invisible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/createButton"
                />-->

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</layout>

The last item (the button) are not showing when i use the app, i scroll and see all the items, except the button.

UPDATE using a useless item after the button with 60dp of weight allows me to scroll and see the button, i don’t like it but seems to be the only solution

UPDATE 2 I put the full xml

Advertisement

Answer

Have you tried making a dummy item ? In other words, add something that is fully transparent (invisible) but will take physical space after the button ? For example, a normal TextView with such parameters:

 <TextView
                android:layout_width="1dp"
                android:layout_height="60dp"
                android:text="-------"
                android:textColor="@color/transparent" <!-- use color code #ff00000 -->
                android:textSize="30sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/yourlastbuttonIDhere"/>

Also, one thing to keep wary of, for best practices: When you’re setting constraints, for example here

                app:layout_constraintTop_toBottomOf="@+id/assignmentSpinner"

You don’t use @+id/assignmentSpinner but rather @id/assignmentSpinner (Remove the plus sign)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement