Zoltan Szilagyi
Zoltan Szilagyi

Reputation: 292

No shadow in appcompat and design library

My problem is there isn't shadow in my app. I am using AppCompat Libary, and Design Library. In preview (android studio) there is shadow, BUT I can't see in my app on the phone.

Here is my layout code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeToolbar" />
    </android.support.design.widget.AppBarLayout>

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

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


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/cards_margin"
                    android:orientation="vertical">


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:lineSpacingMultiplier="1.2"
                        android:text="Data"
                        android:textColor="@color/primary"
                        android:textSize="@dimen/text_big" />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="X: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="Y: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:text="Z: "
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/textViewFragmentHome3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/cards_text_margin"
                            android:lineSpacingMultiplier="1.2"
                            android:textColor="@color/primary_text"
                            android:textSize="@dimen/text_small" />
                    </LinearLayout>

                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

                <com.github.mikephil.charting.charts.LineChart
                    android:id="@+id/lineChartFragmentHome1"
                    android:layout_width="match_parent"
                    android:layout_height="250dp" />

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


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation"
                app:cardUseCompatPadding="true">

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

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show X line"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show Y line"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.AppCompatCheckBox
                        android:id="@+id/checkboxFragmentHome3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="true"
                        android:text="Show Z line"
                        android:textSize="@dimen/text_med" />

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


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/cardViewMarginTopBottom"
                android:layout_marginLeft="@dimen/cardViewMarginSlide"
                android:layout_marginRight="@dimen/cardViewMarginSlide"
                android:layout_marginTop="@dimen/cardViewMarginTopBottom"
                android:padding="@dimen/cards_padding"
                app:cardBackgroundColor="@color/icons"
                app:cardCornerRadius="@dimen/cardCornerRadius"
                app:cardElevation="@dimen/cardElevation"
                app:cardMaxElevation="@dimen/cardMaxElevation">

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

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/switchFragmentHome1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="false"
                        android:text="Show X axis lines number"
                        android:textSize="@dimen/text_med" />

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/switchFragmentHome2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/cards_text_margin"
                        android:checked="false"
                        android:text="Just do it slowly.."
                        android:textSize="@dimen/text_med" />

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

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabFragmentHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/floatingActionButtonMargin"
        android:scaleType="center"
        android:src="@drawable/ic_av_pause"
        app:layout_behavior="com.eternal.arcanus.accelerometer.helpers.FloatingButtonScroll" />
</android.support.design.widget.CoordinatorLayout>

Here is android studio preview:

enter image description here

And the phone shows this:

enter image description here

What I did wrong?

Upvotes: 0

Views: 488

Answers (2)

PPartisan
PPartisan

Reputation: 8231

There is no default shadow unfortunately (or at least as near as I can tell, I am happy to be corrected). However, in your case, there is at least one workaround.

First, create a drawable xml file to represent your shadow (i.e. app_bar_shadow.xml):

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">    
    <gradient
        android:startColor="#02444444"
        android:endColor="#33111111"
        android:angle="90"/>
</shape>

Then add it to your layout, and assign it a scrolling behaviour. Place it just underneath your NestedScrollView in your layout file:

<View
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:id="@+id/app_bar_shadow"
    android:layout_gravity="top"
    android:background="@drawable/app_bar_shadow"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Upvotes: 2

Marco C.
Marco C.

Reputation: 1412

Shadow is available starting from lollipop, are you using a phone/emulator with an older android? Unfortunately appcompat does not solve this issue for older androids

Upvotes: 0

Related Questions