Even though ConstraintLayout in Android is not new, I have only begun to convert my older layouts over as I find time or when creating new layouts, so my experience with them is limited. They don't seem terribly difficult, but I'm struggling with one particular situation.
I have a ListView that is positioned below other elements and it seems that the other elements above push the ListView off screen. I am using an ArrayAdapter to populate the ListView with about 50+ items. In this first example, I lose about 2 items below the screen that I cannot scroll to.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="48dp"
app:layout_constraintTop_toTopOf="parent"
android:queryHint="Search items..." />
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
In this next example, by adding a TextView, it pushes about 10 items off the screen, to which I cannot scroll to.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="48dp"
app:layout_constraintTop_toTopOf="parent"
android:queryHint="Search items..." />
<TextView
android:id="@+id/texty1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:lineSpacingExtra="3sp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView" />
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/texty1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Is it just pure silliness to use a ConstraintLayout here, or is should use a LinearLayout? I have messed with the layout constraints to no avail. If I add layout_constraintBottom_toBottomOf from the ListView it will push the ListView up into the above elements.
You have to add bottom constraint to the ListView but since you also want to make the height wrap_content you have to add:
app:layout_constrainedHeight="true"
app:layout_constraintVertical_bias="0"
ie
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/texty1" />
Read more about Centering position and bias and dimensions constraint