Curved android toolbar with shadow
Asked Answered
B

3

6

I need to have a curved bottom view for Toolbar or CardView.

What I've tried:

bg_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item
            android:bottom="0dp"
            android:left="-100dp"
            android:right="-100dp"
            android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

Setting this as a background to toolbar voids the appbar elevation (shadow). Also if this background is applied to a CardView background is set but the shadow is not according to shape of the background.

See the below image, I want the shadow to wrap around the curve.

Problem is clear in the image

Biggin answered 30/8, 2019 at 8:35 Comment(0)
B
6

I have done this using arcView

Dependency implementation 'com.github.florent37:shapeofview:1.3.2'

<com.github.florent37.shapeofview.shapes.ArcView
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:elevation="5.0dp"
        app:shape_arc_cropDirection="outside"
        app:shape_arc_height="35dp"
        app:shape_arc_position="bottom">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="155dp"
            android:background="@color/colorPrimary"
            android:elevation="5dp" />

    </com.github.florent37.shapeofview.shapes.ArcView>

check attached image

enter image description here

Burgeon answered 30/8, 2019 at 10:23 Comment(1)
Thanks. ArcView did the job for me.Biggin
E
1

Try with below layout

<?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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:elevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/test1"
        android:gravity="center"
        android:padding="@dimen/_10sdp"
        android:text="My Title"
        android:textSize="@dimen/_20ssp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</android.support.constraint.ConstraintLayout>

test1.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item
        android:bottom="0dp"
        android:left="-100dp"
        android:right="-100dp"
        android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item
        android:bottom="10dp"
        android:left="-100dp"
        android:right="-100dp"
        android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent"/>//Background Color
        </shape>
    </item>
</layer-list>

Output:

enter image description here

Elbe answered 30/8, 2019 at 8:47 Comment(3)
Thank you soo much for your answer Mehul. I've tried your code but the view doesn't show any elevation/shadowBiggin
@HamzaAhmedKhan : Just answer update with test.xml please check it. now you can show shadow.Elbe
I appreciate your effort but this more looks like a border/outline to the view not a gradient shadow.Biggin
L
0

you can use following xml file as a back ground and give specific radius as you requirement:

shadowfile.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#18000000" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#05000000" />
            <corners android:radius="7dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#10000000" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#15000000" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#20000000" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <!-- Background -->
    <item>
        <shape>
            <solid android:color="#FFFFFF" />
            <corners android:radius="0dp" />
        </shape>
    </item>
</layer-list>

Note : Here one item equals one layer, you can add or remove as per your requirements

Use in layout or toolbar :

android:background="@drawable/shadowfile"  

from https://mcmap.net/q/1195484/-how-to-make-toolbar-with-rounded-corners-and-elevation-inside-appbarlayout-like-this-picture

Luminescence answered 28/11, 2021 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.