Tutorials Android How to Apply Shadow Effect on Android TextView

How to Apply Shadow Effect on Android TextView

This example explains how to apply Shadow Effect on Android TextView. You can apply Shadow Effect on Android TextView in two ways. Either we do it pragmatically or we can change in the xml layout.

Shadow Effect on Android TextView using XML Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:shadowColor="#000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="50"
        android:text="Text Shadow Example1"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Text Shadow Example2"
        android:textColor="#FBFBFB"
        android:textSize="28dp"
        android:textStyle="bold" />

</LinearLayout>

In the above XML layout code, the textview1 is given with Shadow effect in the layout. below are the configuration items are

  • 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 – it 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

Shadow Effect on Android TextView pragmatically

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

TextView textv = (TextView) findViewById(R.id.textview2);
textv.setShadowLayer(30, 0, 0, Color.RED);

Output

Shadow Effect on Android TextView

Download Source Code

Download complete source code from Github

Total
0
Shares
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
chirag
chirag
4 years ago

why my app crash when using these shadow attributes ?

Chirag
Chirag
4 years ago

Oktypeface was the problem may be shadow dont work with all typeface or may be i declared from resources fonts thats why in styles directly

Previous Post

Create and Manage a New Branch in Git

Next Post

ShareActionProvider Example in Xamarin.Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
2
0
Would love your thoughts, please comment.x
()
x