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...
This question is related to
android
<style name="WhiteTextWithShadow" parent="@android:style/TextAppearance">
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
<item name="android:shadowColor">@android:color/black</item>
<item name="android:textColor">@android:color/white</item>
</style>
then use as
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
tools:text="Today, May 21"
style="@style/WhiteTextWithShadow"/>
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.
android:shadowColor="#94000000"
android:shadowDy="2"
android:shadowRadius="4"
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).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="20dp" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:shadowColor="#000" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="50" android:text="Text Shadow Example1" android:textColor="#FBFBFB" android:textSize="28dp" android:textStyle="bold" /> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Text Shadow Example2" android:textColor="#FBFBFB" android:textSize="28dp" android:textStyle="bold" /> </LinearLayout>
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.
TextView textv = (TextView) findViewById(R.id.textview2); textv.setShadowLayer(30, 0, 0, Color.RED);
Output :
You can do both in code and XML. Only 4 basic things to be set.
e.g.
android:shadowColor="@color/text_shadow_color"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
This draws a prominent shadow on left-lower side of text. In code, you can add something like this;
TextView item = new TextView(getApplicationContext());
item.setText(R.string.text);
item.setTextColor(getResources().getColor(R.color.general_text_color));
item.setShadowLayer(0.01f, -2, 2, getResources().getColor(R.color.text_shadow_color));
Source: Stackoverflow.com