1

I've a constraintLayout which is wrapped with ScrollView in my activity. The activity layout has a GuidleLine on the top, a fragment with height of 0dp and a textView constrained to the bottom of the activity The problem is that the fragment doesn't take the entire space it needs to display it's views completely.

This is the activity layout code:

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

<androidx.core.widget.NestedScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.5" />
            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Upper TEXT"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/guideline"
                app:layout_constraintBottom_toTopOf="@id/nav_host_fragment"/>
            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                android:layout_marginTop="10dp"
                app:layout_constraintBottom_toTopOf="@id/bottomText"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/text"
                app:navGraph="@navigation/nav_graph" />

            <TextView
                android:id="@+id/bottomText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Bottom TEXT"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/nav_host_fragment"
                app:layout_constraintBottom_toBottomOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

This is the fragment 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=".FirstFragment">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button_first"
        android:layout_width="100dp"
        android:layout_height="56dp"
        android:text="Button"
        android:textColor="@color/white"
        android:gravity="center"
        android:backgroundTint="@android:color/holo_red_dark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button2"
        android:layout_width="100dp"
        android:layout_height="56dp"
        android:text="Button 2"
        android:textColor="@color/white"
        android:gravity="center"
        android:backgroundTint="@android:color/black"
        app:layout_constraintTop_toBottomOf="@id/button_first"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button3"
        android:layout_width="100dp"
        android:layout_height="56dp"
        android:text="Button 2"
        android:textColor="@color/white"
        android:gravity="center"
        android:layout_marginTop="100dp"
        android:backgroundTint="@android:color/black"
        app:layout_constraintTop_toBottomOf="@id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button4"
        android:layout_width="100dp"
        android:layout_height="56dp"
        android:text="Button 2"
        android:textColor="@color/white"
        android:gravity="center"
        android:layout_marginTop="50dp"
        android:backgroundTint="@android:color/black"
        app:layout_constraintTop_toBottomOf="@id/button3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The result is that the activity is not scrolling and the fragment is cut.

enter image description here

I tried giving the fragment wrap_content height, but it makes the scrolling very heavy and stugglish and it some cases it's causing other problem.

Please help :-)

Sharas
  • 1,985
  • 3
  • 20
  • 43
  • Can you try chaining the layouts and giving each a weight? You can see [this](https://stackoverflow.com/a/48677954/9722336) for an example to add weight – gtxtreme Sep 03 '21 at 10:01
  • Try adding `MaterialButton` width to `wrap_content` – Mittal Varsani Sep 03 '21 at 10:11
  • @gtxtreme the problem is not with the fragment layout, but with the activity layout – Sharas Sep 03 '21 at 10:41
  • There are several issues with the layout, but you don't really say how it should behave, so it is hard to make suggestions. For instance, should the bottom button scroll with the fragment contents or be pinned to the bottom? What, exactly, should scroll. What is the intention of the guideline? To mark the 50% position on the screen or the 50% mark within the _ConstraintLayout_ which is what it really does. – Cheticamp Sep 03 '21 at 11:56
  • @Cheticamp this is just an example code that represents the real code I have and can't share. The point of the scrollview is to scroll all the activity views including the fragment, so if you scroll from to to bottom, you should see the entire fragment views on top of the bottom textview – Sharas Sep 03 '21 at 14:37
  • And the guideline? Is it to mark the center of the screen or the center of the _ConstraintLayout_? If the latter, then the screen will be blank if the fragment is large enough. – Cheticamp Sep 03 '21 at 14:43
  • @Cheticamp The guideline represents a guideline in my real code that sets the starting point of the views (before the guideline i've a background image) – Sharas Sep 03 '21 at 14:58
  • I ask about the guideline because it is set at 50% of the height of the _ConstraintLayout_. Since you have a _ScrollView_, I am assuming that the _ConstraintLayout_ can push down below the bottom of the screen. The guideline will then shift down to maintain a 50% position within the layout. If the layout is tall enough, the guideline will disappear completely from the screen. Is that your intention? – Cheticamp Sep 03 '21 at 15:39
  • Yes that is my intention – Sharas Sep 03 '21 at 16:24

0 Answers0