Search code examples
androidandroid-layoutscrollviewplaid

How do I move layout on scroll on top of another layout?


I want to move a layout on top of another layout when user scrolls.

for eg. Suppose I have a imageview and below it there is a relative layout inside scrollview. I want to move this relative layout on top on imageview when user scrolls.

Any suggestions?

Edit:-

here 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:orientation="vertical"
    android:background="#e0e0e0"
    android:layout_height="match_parent"
    >


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:title="Watch"
            app:titleMargin="16dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="@color/colorPrimary"
            />

    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView

        android:layout_width="match_parent"
        android:src="@drawable/bg"
        android:scaleType="fitXY"
        android:layout_height="160dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:fastScrollEnabled="true"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >


            <include layout="@layout/content_main" ></include>
    </RelativeLayout>


</ScrollView>
</LinearLayout>

Solution

  • Try the following code. It should work.

    EDITED CODE

    I have tested the below code. The textview scrolls above the image now. Replace the textview with your content by using the include tag.

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:title="Watch"
            app:titleMargin="16dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="@color/colorPrimary"
            />
    
    </android.support.design.widget.AppBarLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:scaleType="fitXY"
            android:src="@drawable/bg" />
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollEnabled="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true">
         <include layout="@layout/content_main" ></include>
        </ScrollView>
    
    </RelativeLayout>
    </RelativeLayout>