15

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?

Community
  • 1
  • 1
User
  • 31,811
  • 40
  • 131
  • 232

1 Answers1

54

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

Iman Marashi
  • 5,593
  • 38
  • 51
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46