Search code examples
androidxmlscrollviewbottomnavigationview

BottomNavigationView moves up when other views are smaller than one screen


I have an imageView, ProgressBar, TextView and RecyclerView in a ConstraintLayout and I want to make it scrollable, but I don't want my BottomNavigationView to move. I am not sure how to make it stay down, when the content is smaller than one screen. I tried similar solutions proposed here, like NestedScrollView, but they just make some of the views disappear.

I don't know if my code pastes well, so here is the Github link: https://github.com/PiotrDawidziuk/QuizO2Api/blob/master/app/src/main/res/layout/activity_take_quiz.xml

my code:

<?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"
tools:context=".activities.DetailActivity">
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.TakeQuizActivity">

<ProgressBar
    android:id="@+id/simpleProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
    />

<ImageView
    android:id="@+id/take_quiz_question_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/message"
    app:layout_constraintStart_toStartOf="parent"
    />

<android.support.v7.widget.RecyclerView
    android:id="@+id/take_quiz_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintEnd_toEndOf="parent"
    >
</android.support.v7.widget.RecyclerView>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu"/>
 </android.support.constraint.ConstraintLayout>
</ScrollView>

I changed the code a little bit, I put the ScrollView inside the ConstraintLayout and put a LinearLayout around everything else. It scrolls a little bit, but it cuts a bottom part of the RecyclerView for some reason:

<?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"
tools:context=".activities.DetailActivity">
<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    tools:context=".activities.TakeQuizActivity"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <ProgressBar
        android:id="@+id/simpleProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
        />

    <ImageView
        android:id="@+id/take_quiz_question_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/message"
        app:layout_constraintStart_toStartOf="parent"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/take_quiz_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        >
    </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</ScrollView>
<android.support.design.widget.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu"/>
</android.support.constraint.ConstraintLayout>

Solution

  • You should move scrollView inside the constaraintLayout.However, in your code, it is the opposite. Look at it

     <?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">
        <ScrollView
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
        >
    
            <android.support.constraint.ConstraintLayout android:layout_width="match_parent"
                                                               android:layout_height="wrap_content">
                <ProgressBar
                        android:id="@+id/simpleProgressBar"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                />
    
                <TextView
                        android:id="@+id/message"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/simpleProgressBar"
                />
    
                <ImageView
                        android:id="@+id/take_quiz_question_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toBottomOf="@+id/message"
                        app:layout_constraintStart_toStartOf="parent"
                />
    
                <android.support.v7.widget.RecyclerView
                        android:id="@+id/take_quiz_recycler_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_constraintTop_toBottomOf="@+id/take_quiz_question_image"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintBottom_toTopOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                >
                </android.support.v7.widget.RecyclerView>
            </android.support.constraint.ConstraintLayout>
    
        </ScrollView>
        <android.support.design.widget.BottomNavigationView
                android:id="@+id/nav_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginEnd="0dp"
                android:background="?android:attr/windowBackground"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:menu="@menu/bottom_nav_menu"/>
    </android.support.constraint.ConstraintLayout>