Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEric Drake
6,339 PointsSize one element to fill the remaining space on the screen
I'm working on an app that has a few EditText elements and buttons. It also has a NestedScrollView that I would like to size to fill whatever space remains on the screen. It should stretch from the bottom of my nameSaveButton to my startDrawingButton. Can anyone help me with this?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:hint="@string/name_entry_hint"
android:inputType="textPersonName"
android:maxLength="35"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/scoreEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:hint="@string/score_entry_hint"
android:inputType="numberSigned"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/nameEditText" />
<Button
android:id="@+id/nameSaveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/entry_save_button"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/scoreEditText" />
<android.support.v4.widget.NestedScrollView
android:id="@+id/namesListView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1"
android:fillViewport="true"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@id/nameSaveButton">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</android.support.v4.widget.NestedScrollView>
<Button
android:id="@+id/startDrawingButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Button"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
1 Answer
Eric Drake
6,339 PointsI found an answer. Hopefully, it will be useful to someone else. Here is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:hint="@string/name_entry_hint"
android:inputType="textPersonName"
android:maxLength="35"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/scoreEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:hint="@string/score_entry_hint"
android:inputType="numberSigned"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/nameEditText" />
<Button
android:id="@+id/nameSaveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/entry_save_button"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/scoreEditText" />
<android.support.v4.widget.NestedScrollView
android:id="@+id/namesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:background="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@id/nameSaveButton"
app:layout_constraintBottom_toTopOf="@id/startDrawingButton">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</android.support.v4.widget.NestedScrollView>
<Button
android:id="@+id/startDrawingButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Button"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
The actual fix is applied to the NestedScrollView. You'll notice that I've added two constraints: app:layout_constraintTop_toBottomOf="@id/nameSaveButton" and app:layout_constraintBottom_toTopOf="@id/startDrawingButton". Then I set the android:layout_height="0dp" to force Android to calculate the height of the NestedScrollView at runtime.