I am trying to create a square LinearLayout container of id imgContainer within a ScrollView element, but I am unable to do so because the app:layout_constraintDimensionRatio doesn't seem to be working. Using the following xml code, the imgContainer renders with a height of 0dp:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/imgContainer"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/trackName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_weight="1"
android:text="Test Name"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgContainer" />
<!-- Other stuff -->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
However, when I move the imgContainer out of the ScrollView like so, the imgContainer is square-shaped as expected.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/imgContainer"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
...>
<!-- Same as above -->
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
I also tried moving out of the LinearLayout it is nested in while still having it within the ScrollView, but that didn't work either. If anyone knows how to fix this, and why this happens, that would be great. Thanks in advance.
app:layout_constraint.. only work under <androidx.constraintlayout.widget.ConstraintLayout
you can try the following:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/imgContainer"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/trackName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_weight="1"
android:text="Test Name"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgContainer" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Related
The issue is the background of my scrollview is somewhat have pretty same content as my scroll view. i have try looking but can't found the problem, because every place tell that i just have to place the content into the scrollview. Please Help!
The design of the layout
This is my layout code
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_rv"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:elevation="3dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout"
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/category_rv">
<include layout="#layout/horizontal_scroll_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout1"
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/LinearLayout">
<include layout="#layout/sliding_ad_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LinearLayout1">
<include layout="#layout/grid_product_layout" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Put recyclerview and scrollview into nestedscrollview
This will solve your problem.
Add constraints to your scroll view, not to LinearLayout
<ScrollView
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/category_rv">
Could you guide me how to put 4 colors in linearlayout instead of create 4 views of linearlayout because I realized there is better way but I don't know how to apply it.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The result should be like image below
Better to use ConstraintLayout
<?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">
<ImageView
android:id="#+id/imgOne"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff00"
app:layout_constraintBottom_toTopOf="#+id/imgThree"
app:layout_constraintEnd_toStartOf="#id/imgTwo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgTwo"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#2196F3"
app:layout_constraintBottom_toTopOf="#+id/imgFour"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#id/imgOne"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgThree"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FFC107"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/imgTwo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgOne" />
<ImageView
android:id="#+id/imgFour"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF5722"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#id/imgThree"
app:layout_constraintTop_toBottomOf="#+id/imgTwo" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
Use Constraint Layout like this
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/dark_blue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/clThird"
app:layout_constraintEnd_toStartOf="#id/clSecond"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/clFirst">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/light_gray"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/clFourth"
app:layout_constraintStart_toEndOf="#id/clFirst"
android:id="#+id/clSecond">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/clFourth"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/clFirst"
android:id="#+id/clThird">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/blue_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/clSecond"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/clThird"
android:id="#+id/clFourth">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use Linear Layout for vertical and Horizontal orientation with weight 1 like below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey3" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/grey4" />
</LinearLayout>
</LinearLayout>
I have tried the below code but all the time its giving me the recyclerview as full screen any idea how to solve this.
Note i have only two rows/items in my recyclerview
<?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"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recyclerview_item" />
</RelativeLayout>
<EditText
android:id="#+id/textfield"
android:layout_width="294dp"
android:layout_height="53dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="164dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
Yes you can do it with constraint layout here is the code you can follow
Note: Please do clean & Rebuild Project to reflect the correct UI changes i have noticed in Android Studio 3.5+ sometimes the UI doesn't reflect.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="0dp"
tools:listitem="#layout/recyclerview_item"
app:layout_constraintBottom_toTopOf="#id/textfield"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/textfield"
android:layout_width="196dp"
android:layout_height="56dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="164dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/recyclerview1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Easiest way to implement this by using relativelayout.
Just replace your code with this.
<RelativeLayout
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.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/recyclerview_item"
android:layout_above="#id/textfield"//Recyclerview will take above area.
/>
<EditText
android:id="#+id/textfield"
android:layout_width="294dp"
android:layout_height="53dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginBottom="164dp"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
Try below layout:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="#+id/recyclerview_layout"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recyclerview_item" />
</RelativeLayout>
<EditText
android:id="#+id/textfield"
android:layout_width="294dp"
android:layout_height="53dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
app:layout_constraintTop_toBottomOf="#+id/recyclerview_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerview_item should use wrap_content as layout_height
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <!--Should be wrap_content-->
<!--Here list item content-->
</LinearLayout>
Output:
You can also achieve this by simply use LinearLayout like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="#+id/recyclerview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recyclerview_item" />
</RelativeLayout>
<EditText
android:id="#+id/textfield"
android:layout_width="294dp"
android:layout_height="53dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp">
</EditText>
</LinearLayout>
Here is how to do it correctly:
<?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.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="0dp"
tools:listitem="#layout/recyclerview_item"
app:layout_constraintBottom_toTopOf="#id/textfield"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/textfield"
android:layout_width="294dp"
android:layout_height="53dp"
android:layout_marginStart="80dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="164dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/recyclerview1" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope this is what you are looking for.
scroll view not working when implementing like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/Constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1.05" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
i saw a post where we can anchor the elements to the guideline by increasing it beyond the 100% but when i am using this the scroll isn't working even the textview isn't visible anymore how do i fix this
Actually, your code should work when there is enough elements to scroll, try to add a higher margintop to the text view and you will see the result:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:gravity="center_horizontal"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="630dp"
android:layout_marginBottom="32dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1.0"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginStart="8dp"
android:layout_marginTop="600dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Can you have a try with BottomSheet ?
add the below linearlayout into scrollview and add your constraint layout in this linearlayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
i think it will work
I have a Fragment composed of three subfragments :
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<fragment
android:id="#+id/header"
class="xxx.xxx.xxx.HeaderFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/content"
class="xxx.xxx.xxx.ContentFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/footer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/header" />
<fragment
android:id="#+id/footer"
class="xxx.xxx.xxx.FooterFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxHeight="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>
This way, the ContentFragment fills the space between HeaderFragment and FooterFragment, as shown in the picture below :
green = header
blue = content
red = footer
Now I would like to add two views to the ContentFragment layout (one TextView and one ScrollView), so that those views take the whole blue space available.
Here is the layout for ContentFragment :
<?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:background="#color/lightBlue">
<TextView
android:background="#color/white"
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="#id/right"
app:layout_constraintStart_toStartOf="#id/left"
app:layout_constraintTop_toTopOf="parent"/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#id/right"
app:layout_constraintStart_toStartOf="#id/left"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:id="#+id/veryLongTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<android.support.constraint.Guideline
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />
<android.support.constraint.Guideline
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />
</android.support.constraint.ConstraintLayout>
This is what I get instead :
Can someone explain why there is some space between the bottom of the scrollview and the bottom of the blue part ? It is not what I have defined in the constraints, which is rendered like this in Android Studio :
I am still unsure why this is happening. My guess is that Android cannot compute the right height for the ContentFragment at runtime, because the problem disappears if I define the fragments height using Guidelines as shown below :
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<fragment
android:id="#+id/header"
class="xxx.xxx.xxx.HeaderFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/topLimit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/content"
class="xxx.xxx.xxx.ContentFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomLimit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/topLimit" />
<fragment
android:id="#+id/footer"
class="xxx.xxx.xxx.FooterFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/bottomLimit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.constraint.Guideline
android:id="#+id/topLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.15" />
<android.support.constraint.Guideline
android:id="#+id/bottomLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.85" />
</android.support.constraint.ConstraintLayout>
Which is rendered like this :