2

I have tried to accomplish something similar to the following layout.

The card is overlapping the appbarlayout

There are a few things to note. The cardview that overlaps the appbar is not supposed to be a part of a nestedscrollview as I have a nestedscrollview in the xml layout. The cardview is positioned correctly but it comes just beneath the appbarlayout.

Samuel Agbede
  • 380
  • 1
  • 4
  • 14
  • What makes you think that's an AppbarLayout instead of a CardView with a custom background? – OneCricketeer Nov 21 '17 at 14:10
  • @cricket_007 I am trying to implement a CardView with an AppBarLayout. I want to add a behaviour that makes the cardview fade out as the user scrolls down. Thanks. – Samuel Agbede Nov 21 '17 at 14:12
  • Overlap can be gotten here https://stackoverflow.com/questions/31031918/overlaying-content-above-appbarlayout-using-new-material-design – OneCricketeer Nov 21 '17 at 14:17
  • @cricket_007, thanks. I had seen this other question before I asked. It doesn't work for me. I don't want the nestedscrollview overlapping the appbarlayout. I want just the cardview. – Samuel Agbede Nov 21 '17 at 20:39
  • You can apply the same idea or XML attributes, no? – OneCricketeer Nov 21 '17 at 22:23
  • @cricket_007 I do not understand. Apply the same idea (from the link you sent to me) to this particular problem. – Samuel Agbede Nov 22 '17 at 10:31

2 Answers2

3

I just got the solution to this now. I did not give the CardView an elevation value before and that was wrong. I just assumed that making it come last in the CoordinatorLayout should place it on top. It works now. I have added an elevation value. Here is the code -

<xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbarlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/quotes"
                android:scaleType="centerCrop"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/tool"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax"/>


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

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



    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginRight="32dp"
        android:layout_marginLeft="32dp"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|center"
        app:cardElevation="@dimen/small_padding"
        />

</android.support.design.widget.CoordinatorLayout>
Samuel Agbede
  • 380
  • 1
  • 4
  • 14
  • Thank you for your answer, it save my time :) Also add " app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" to collapse image in – Ramkesh Yadav Jan 23 '20 at 09:01
1

Try with the following layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways"
        app:layout_collapseMode="pin">

        <ImageView
            android:id="@+id/toolbar_logo"
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"/>

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginLeft="45dp"
        android:layout_marginRight="45dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="7dp"
        android:layout_marginTop="150dp"
        android:id="@+id/cardView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp">

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Photos" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="376" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Followers"
                    android:layout_gravity="center"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1769"
                    android:layout_gravity="center"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Following"
                    android:layout_gravity="center"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="127"
                    android:layout_gravity="center"/>
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>
Praveen
  • 220
  • 1
  • 2
  • 15
  • This is a good way of accomplishing this. The only issue is that this is not using a coordinator layout. I want the card at the top to fade out when the user scrolls down. Thanks. – Samuel Agbede Nov 22 '17 at 18:45