Search code examples
androidxmlandroid-linearlayoutnestedscrollview

LinearLayout and NestedScrollView problem


How I want to use NestedScrollView, the view for the toolbar at the top and the bottom View view at the bottom. The top and bottom view Toolbar and bottom view must always be in place.

How do I achieve this result for any device? I appreciate any answer, thank you.

Always like that:

enter image description here

It is my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
    <!--This is top-->
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/colorPrimary">
        <!--.........................-->
    </androidx.appcompat.widget.LinearLayoutCompat>


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/colorGreyFragment">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <!--This is bottom-->
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottom_buttons_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="@color/colorWhite">
        <!--..............................-->
    </androidx.appcompat.widget.LinearLayoutCompat>

</LinearLayout>

Solution

  • You can do it this way,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical">
        <!--This is top-->
        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary">
            <!--.........................-->
        </androidx.appcompat.widget.LinearLayoutCompat>
    
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorGreyFragment"
                android:orientation="vertical">
    
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"></androidx.constraintlayout.widget.ConstraintLayout>
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    
        <!--This is bottom-->
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    I have replaced your linear layout at the bottom with the BottomNavigationView, if you want some other thing you can replace there.