Shadow Effect for a Text in Android?

Possible Duplicate:
Android - shadow on text?

How can i make shadow effect text in a TextView.

Any Idea?


Solution 1:

put these in values/colors.xml

<resources>
    <color name="light_font">#FBFBFB</color>
    <color name="grey_font">#ff9e9e9e</color>
    <color name="text_shadow">#7F000000</color>
    <color name="text_shadow_white">#FFFFFF</color>
</resources>

Then in your layout xml here are some example TextView's

Example of Floating text on Light with Dark shadow

<TextView android:id="@+id/txt_example1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textSize="14sp"
                  android:textStyle="bold"
                  android:textColor="@color/light_font"
                  android:shadowColor="@color/text_shadow"
                  android:shadowDx="1"
                  android:shadowDy="1"
                  android:shadowRadius="2" />

enter image description here

Example of Etched text on Light with Dark shadow

<TextView android:id="@+id/txt_example2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:textColor="@color/light_font"
                android:shadowColor="@color/text_shadow"
                android:shadowDx="-1"
                android:shadowDy="-1"
                android:shadowRadius="1" />

enter image description here

Example of Crisp text on Light with Dark shadow

<TextView android:id="@+id/txt_example3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:textColor="@color/grey_font"
                android:shadowColor="@color/text_shadow_white"
                android:shadowDx="-2"
                android:shadowDy="-2"
                android:shadowRadius="1" />

enter image description here

Notice the positive and negative values... I suggest to play around with the colors/values yourself but ultimately you can adjust these settings to get the effect your looking for.

Solution 2:

Perhaps you'd consider using android:shadowColor, android:shadowDx, android:shadowDy, android:shadowRadius; alternatively setShadowLayer() ?

Solution 3:

TextView textv = (TextView) findViewById(R.id.textview1);
textv.setShadowLayer(1, 0, 0, Color.BLACK);

Solution 4:

Complete answer

Add these 2 line to the parent view otherwise Text Shadow will be clipped when view bound are smaller than shadow offset

android:clipChildren="false"

android:clipToPadding="false"

EXAMPLE:

<androidx.constraintlayout.widget.ConstraintLayout
            android:clipChildren="false"
            android:clipToPadding="false"
           ... >

        <TextView
         style="@style/TextShadowStyle"
          ....
            />

Text Shadow Style:

 <style name="TextShadowStyle">
        <item name="android:shadowColor">@color/black</item>
        <item name="android:shadowDx">10</item>
        <item name="android:shadowDy">10</item>
        <item name="android:shadowRadius">5</item>
    </style>