Nitesh Kumar
Nitesh Kumar

Reputation: 5440

Drop shadow on AppBarLayout

I have used Design Support Library's AppBarLayout to show Toolbar and TabLayout in my project.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="#FF0000">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:scrollbars="horizontal"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />

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

This AppBarLayout cast a drop shadow in Lollipop, however, there is no drop shadow in pre-lollipop devices.

How can I show the drop shadow in pre-lollipop devices as well?

Upvotes: 1

Views: 4800

Answers (4)

user2212515
user2212515

Reputation: 1220

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/ab_shadow_height"
                android:background="@drawable/sh_bar" />
        </FrameLayout>

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

Upvotes: 0

Sourav Chakraborty
Sourav Chakraborty

Reputation: 9

Create an xml file under drawable and name it custom_shadow.xml. Now copy and paste the following code..

<gradient
    android:centerColor="@android:color/transparent"
    android:endColor="@android:color/transparent"
    android:startColor="@android:color/transparent" />

<corners android:radius="0dp" />

Upvotes: 0

Sourav Chakraborty
Sourav Chakraborty

Reputation: 9

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

            <application.project.freelincerapp.tabs.SlidingTabLayout
                android:id="@+id/slidingTabLayout"
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:layout_height="wrap_content" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appBarLayout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <View
            android:id="@+id/toolbar_shadow"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:alpha="0.90"
            android:layout_below="@id/appBarLayout"
            android:background="@drawable/custome_shadow"/>


    </RelativeLayout>

Upvotes: 0

Darshan Mistry
Darshan Mistry

Reputation: 3372

Do note this only works for lollipop and above. It won't work pre lollipop device so you have to create custom xml for shadow like below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_alizarin"
        android:titleTextAppearance="@color/White"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- **** Place Your Content Here **** -->

        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@drawable/toolbar_dropshadow" />
    </FrameLayout>
</LinearLayout>



 @drawable/toolbar_dropshadow:
  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="@android:color/transparent"
        android:endColor="#88333333"
        android:angle="90"/>
    </shape>

@color/color_alizarin

<color name="color_alizarin">#e74c3c</color>

Upvotes: 4

Related Questions