I am wondering how to add shadow on text in android?
I have the following code which is applied on a bitmap and I wanted to be shadowed...
paint.setColor(Color.BLACK);
paint.setTextSize(55);
paint.setFakeBoldText(false);
paint.setShadowLayer(1, 0, 0, Color.BLACK); //This only shadows my whole view...
Draw 2 texts: one gray (it will be the shadow) and on top of it draw the second text (y coordinate 1px more then shadow text).
You can do both in code and XML. Only 4 basic things to be set.
e.g.
This draws a prominent shadow on left-lower side of text. In code, you can add something like this;
If you want to achieve a shadow like the one that Android does in the Launcher, we're managing these values. They're useful if you want to create TextViews that will appear as a Widget, without a background.
You should be able to add the style, like this (taken from source code for Ringdroid):
And in your layout, use the style like this:
Edit: the source code can be viewed here: https://github.com/google/ringdroid
Edit2: To set this style programmatically, you'd do something like this (modified from this example to match ringdroid's resources from above)
The signature for
setTextAppearance
isthen use as
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.
Output :