How to add shadow to TextView in Android Android 24.04.2017

In this example you will learn how to apply shadow effect on TextView in Android. You can apply shadow to TextView in two ways: either pragmatically or in the xml layout.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shadowColor="@android:color/white"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="50"
        android:text="The Godfather"
        android:textColor="@android:color/white"
        android:textSize="40dp"
        android:textStyle="bold" />

</LinearLayout>    

In the above XML layout code, the TextView is given with shadow effect in the layout. Below are the configuration items

  • android:shadowDx specifies the X-axis offset of shadow. You can give -/+ values, where -Dx draws a shadow on the left of text and +Dx on the right.
  • android:shadowDy specifies the Y-axis offset of shadow. -Dy specifies a shadow above the text and +Dy specifies below the text.
  • android:shadowRadius specifies how much the shadow should be blurred at the edges. Provide a small value if shadow needs to be prominent.
  • android:shadowColor specifies the shadow color.

Use below code snippet to get the shadow effect on the second TextView pragmatically.

TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
tvTitle.setShadowLayer(50, 0, 0, Color.RED);

Result

android_shadow.png

Useful links