Can't set TextView shadow programmatically

Ixx picture Ixx · Aug 8, 2012 · Viewed 10.9k times · Source

I'm creating a TextView dynamically and set shadow to it using the method posted here: Android - shadow on text?

But it doesn't work. The style is applied (put textSize item to test, and it works), but the shadow doesn't appear.

TextView:

TextView tv = new TextView(this);
RelativeLayout.LayoutParams layoutPars = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutPars.addRule(RelativeLayout.CENTER_VERTICAL); 
tv.setTextColor(0xffffffff);
tv.setText(label);
tv.setTextSize(11);
tv.setTextAppearance(getApplicationContext(), R.style.BlackShadow);

Style:

<style name="BlackShadow">  
    <item name="android:shadowColor">#ff000000</item>
    <item name="android:shadowRadius">1</item>
    <item name="android:shadowDx">-1</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:textSize">26dip</item>
</style>

What am I doing wrong?

Answer

Deepak Swami picture Deepak Swami · Aug 8, 2012

Try this:

tv.setShadowLayer(1.5f, -1, 1, Color.LTGRAY);

From documentation

setShadowLayer(float radius, float dx, float dy, int shadowColor)

This draws a shadow layer below the main layer, with the specified offset and color, and blur radius.

For more information please check http://developer.android.com/reference/android/graphics/Paint.html#setShadowLayer%28float,%20float,%20float,%20int%29