Home > Software design >  Add ScrollView with Linear and Relative Layout
Add ScrollView with Linear and Relative Layout

Time:08-18

Hello I am trying to add a ScrollView in my project.

The purpose is that I will be putting multiple CardViews in the middle of my project.

It is within a Relative Layout. With a Linear Layout on the top portion. But also having a BottomNavigation at the bottom. Which I would like for it to be fixed and not scrolling.

Here is the XML Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Bottom_Nav"
android:fillViewport="true"
android:background="@drawable/bg7">

<LinearLayout
    android:id="@ id/titlemenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/pacifico_regular"
    android:text="Menu"
    android:textColor="#6E2C00"
    android:textSize="50dp"
    android:textStyle="bold"
    android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/carterone_regular"
        android:text="View Our Seelction!"
        android:textColor="#6E2C00"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_gravity="center"/>
</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/titlemenu"
    android:layout_above="@id/bottom_navigation">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <androidx.cardview.widget.CardView
            android:id="@ id/hot"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            app:cardCornerRadius="15dp"
            android:layout_marginTop="10dp"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#EB984E"
                android:padding="10dp">

                <TextView
                    android:id="@ id/t1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:fontFamily="@font/carterone_regular"
                    android:text="Cold Drinks"
                    android:textColor="#6E2C00"
                    android:textSize="30dp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/t1"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:fontFamily="@font/carterone_regular"
                    android:text="Cool Off With Our Cold Bevs!"
                    android:textStyle="bold" />

                <ImageView
                    android:layout_width="116dp"
                    android:layout_height="82dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="-31dp"
                    android:maxHeight="200dp"
                    android:src="@drawable/coldbev" />


            </RelativeLayout>

        </androidx.cardview.widget.CardView>


    </LinearLayout>
    <Button
        android:id="@ id/coldbutton"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:backgroundTint="#6E2C00"
        android:textColor="@color/white"
        android:text="View Cold Drinks"
        android:layout_gravity="center"/>



</ScrollView>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@ id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#EB984E"
    app:itemIconTint="#6E2C00"
    app:itemTextColor="#6E2C00"
    app:menu="@menu/menu_navigation" />


 </RelativeLayout>

As of right now it looks like this : https://ibb.co/4JP6cPP

CodePudding user response:

just put scrollView in root view

<?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:orientation="vertical"
tools:context=".Bottom_Nav"
android:fillViewport="true"
android:background="@drawable/bg7">

<LinearLayout
    android:id="@ id/titlemenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/pacifico_regular"
    android:text="Menu"
    android:textColor="#6E2C00"
    android:textSize="50dp"
    android:textStyle="bold"
    android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/carterone_regular"
        android:text="View Our Seelction!"
        android:textColor="#6E2C00"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_gravity="center"/>
</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/titlemenu"
    android:layout_above="@id/bottom_navigation">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <androidx.cardview.widget.CardView
            android:id="@ id/hot"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            app:cardCornerRadius="15dp"
            android:layout_marginTop="10dp"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#EB984E"
                android:padding="10dp">

                <TextView
                    android:id="@ id/t1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:fontFamily="@font/carterone_regular"
                    android:text="Cold Drinks"
                    android:textColor="#6E2C00"
                    android:textSize="30dp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/t1"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:fontFamily="@font/carterone_regular"
                    android:text="Cool Off With Our Cold Bevs!"
                    android:textStyle="bold" />

                <ImageView
                    android:layout_width="116dp"
                    android:layout_height="82dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="-31dp"
                    android:maxHeight="200dp"
                    android:src="@drawable/coldbev" />


            </RelativeLayout>

        </androidx.cardview.widget.CardView>


    </LinearLayout>
    <Button
        android:id="@ id/coldbutton"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:backgroundTint="#6E2C00"
        android:textColor="@color/white"
        android:text="View Cold Drinks"
        android:layout_gravity="center"/>



</ScrollView>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@ id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#EB984E"
    app:itemIconTint="#6E2C00"
    app:itemTextColor="#6E2C00"
    app:menu="@menu/menu_navigation" />


 </ScrollView>
  • Related