Shadow Effect for a Text in Android

Shadow Effect for a Text in Android?

Perhaps you'd consider using android:shadowColor, android:shadowDx, android:shadowDy, android:shadowRadius; alternatively setShadowLayer() ?

Android - shadow on text?

You should be able to add the style, like this (taken from source code for Ringdroid):

  <style name="AudioFileInfoOverlayText">
<item name="android:paddingLeft">4px</item>
<item name="android:paddingBottom">4px</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:textSize">12sp</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
</style>

And in your layout, use the style like this:

 <TextView android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/AudioFileInfoOverlayText"
android:gravity="center" />

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)

TextView infoTextView = (TextView) findViewById(R.id.info);
infoTextView.setTextAppearance(getApplicationContext(),
R.style.AudioFileInfoOverlayText);

The signature for setTextAppearance is

public void setTextAppearance (Context context, int resid)

Since: API Level 1

Sets the text color, size, style, hint color, and
highlight color from the specified TextAppearance resource.

How do I Set the Shadow around TextView

Create a new drawable resource file. I named mine two_sided_border.xml.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
<shape>
<solid android:color="@color/black" />
</shape>
</item>
<!-- This is the main color -->
<item android:bottom="2dp" android:right="2dp">
<shape>
<solid android:color="@color/green" />
</shape>
</item>
</layer-list>

Then use this as the background for your textview.

<TextView
android:id="@+id/t"
android:background="@drawable/two_sided_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>

Your colors and dimensions (padding, text size) can be tweaked to suit.

Two great links:

how to make stroke for 3 sides for a shape in android?

And borrowed some code from here:

Open-sided Android stroke?

ps the other answer provided by Basil Miller gives good way to set shadow for the actual text itself.

textview

How can make Text Shadow in android?

Try below code

 <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="@color/text_shadow"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:text="Hello Shadow Text"
android:textColor="@android:color/black"
android:textSize="14sp"
android:textStyle="bold" />

and define color as

  <color name="text_shadow">#58ACFA</color>

Android How does TextView Shadow work

Please Refer the following link:

http://android--code.blogspot.in/2015/05/android-textview-text-shadow.html

You have to take the 3-4 textview's in yout xml file, then change the shadowDx, shadowDy and color of the shadow for each textview, then you will observe the difference.

Background shadow in android

New and Best Approach (I hope that's what you required)

Sample Image

 <com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordTV"
style="@style/Widget.App.TextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/text_input_selector"
android:backgroundTint="#0B102C"
android:outlineAmbientShadowColor="#1EB0D5"
android:outlineSpotShadowColor="#1EB0D5"
android:textColorHint="#4A5478"
android:translationZ="@dimen/_10sdp"
app:endIconDrawable="@drawable/ic_show_password"
app:endIconMode="password_toggle"
app:hintTextColor="#4A5478">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordEt"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:drawableStart="@drawable/ic_lock"
android:drawablePadding="7dp"
android:hint=" Password"
android:inputType="textPassword"
android:textColor="@color/white"
android:textColorHint="#CAD5F4"
android:textSize="12dp" />

</com.google.android.material.textfield.TextInputLayout>
  • you just create a text_input_selector in drawable


     <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:state_focused="true"
    android:drawable="@drawable/background_glow"/>
    </selector>

    text_input_selector.xml

  • Now create a shape for background glow in drawable

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0C263E"/>
<corners android:radius="7dp"/>
</shape>

background_glow.xml

Add drop shadow effects to EditText Field

Well.. @Shalini's answer helped me in this way but still I got another way to achieve 2D shadow with EditText Field and I am going to share with you.

We need to create custom XML view with three layer for EditText,
bottom shadow and right side shadow

Below is my code.

res/drawable/edittext_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- most important is order of layers -->

<!-- Bottom right side 2dp Shadow -->
<item >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>

<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>

<!-- White Top color -->
<item android:bottom="3px" android:right="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>

Now we can set this shadow view to our TextField using "Background" property

like this

res/layout/main.xml

<EditText android:layout_width="wrap_content" 
android:id="@+id/txtpin"
android:maxLength="4"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:longClickable="false"
android:padding="2dp"

android:inputType="textPassword|number"
android:password="true"
android:background="@drawable/edittext_shadow"
android:layout_weight="0.98"
android:layout_marginLeft="15dp">
<requestFocus></requestFocus>
</EditText>

and the result screen is like I have posted in question above.

Thanks to SO, sharing knowledge.

Shadow effect of TextView in Android Studio doesn't work

Well, thank you guys for your contributions. I played around with the code and put a larger value to radius added a black background to the TextView and voila...the shadow!



Related Topics



Leave a reply



Submit