Add shadow to a label text?

Inman

Well-Known Member
Licensed User
I have a label with gray color text and transparent background. The label rests on top of an Imageview. Since the user can load any image and because the label background is transparent, the label text may not always be visible. One solution to this is to add a shadow to the text.

I believe Android allows this. There is a discussion at Stack Overflow:

Android - shadow on text? - Stack Overflow

Could you please convert this code to B4A?
 

corwin42

Expert
Licensed User
You can do it with reflection library.

B4X:
Sub SetTextShadow(pView As View, pRadius As Float, pDx As Float, pDy As Float, pColor As Int)
   Dim ref As Reflector
   
   ref.Target = pView
   ref.RunMethod4("setShadowLayer", Array As Object(pRadius, pDx, pDy, pColor), Array As String("java.lang.float", "java.lang.float", "java.lang.float", "java.lang.int"))
End Sub
 
Upvote 0

Douglas Farias

Expert
Licensed User
how to use
i m trying this but dont work
SetTextShadow(labelfoto,200,500,500,Colors.Blue)

*-*


edit

B4X:
    SetTextShadow(labelfoto,5,0,1,color)
    SetTextShadow(labelbackground,5,0,1,color)

thsi works
 
Last edited:
Upvote 0

ivavilagu

Member
Licensed User
I´ve got the error : NoSuchMethodExcepption: setShadowLayer

Using 2.40 Reflection Version
 
Upvote 0

corwin42

Expert
Licensed User
I´ve got the error : NoSuchMethodExcepption: setShadowLayer

Using 2.40 Reflection Version

On what view do you try it? It only works on Views which are based on TextView.
 
Upvote 0

ivavilagu

Member
Licensed User
Trying on a panel view
 
Upvote 0

Troberg

Well-Known Member
Licensed User
A simple fix that works is to add a second label, below the main label, and with thext color in the color you want the shadow to have. Place it so that it's one pixel to the left and one pixel below the main label. Add the same text to that.

Not as neat, but it should work on all platforms.

Another solution, though different in effect, is to make sure the label is sized to just fit the text, and then have a semi-transparent background on the label. That way, you'll get an effect like TV subtitles.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
You can do it with reflection library.

Ah, just what I was looking for. Thanks!

Over time, I would love to see @Erel add these kinds of additional Android properties to B4A, so we can set them easily from the designer or our code without using reflection.
 
Upvote 0
Top