Thursday, August 4, 2016

android - How to add shadow on a drawable

activity_main.xml

<?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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f4f2ef"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:textSize="40sp"
        android:background="@drawable/circle_shape"
        android:padding="25dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Drawable Shadow"
        />
</android.support.design.widget.CoordinatorLayout>
res/drawable/circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- draw a shadow in background -->
    <item>
        <shape android:shape="oval">
            <gradient
                android:startColor="#000000"
                android:endColor="#c9c9c9"
                android:angle="90"
                />
        </shape>
    </item>

    <!-- draw a circular shape with border -->
    <item
        android:right="5dp"
        android:bottom="5dp"
        >
        <shape android:shape="oval">
            <solid android:color="#259c21"/>
            <stroke android:color="#ba0a89" android:width="10dp"/>
        </shape>
    </item>
</layer-list>