user6731698
user6731698

Reputation: 67

Adding shadow to Rounded corner TextView?

I want to add shadow to my text view . I found solutions for adding gradients and one other solution which suggested creating a duplicate text view with a lighter shade at the back of the original textView to give a shadow effect. But i want to have a real shadow effect.

This is what i tried .I created an xml file rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="#33000000"
        android:dashWidth="3dp"
        />

    <solid android:color="#000000" />
    <padding

        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners android:radius="10dp"
        />

</shape>

I added elevation and translationz but its not showing shadow in the output

<TextView
        android:background="@drawable/rounded_corners"

        android:layout_width="260dp"
        android:layout_height="70dp"
        android:textSize="50sp"
        android:text="Welcome User"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:textColor="#000000"
        android:elevation="50dp"
        android:translationZ="10dp"
        android:id="@+id/welcome"/>

Upvotes: 1

Views: 9426

Answers (3)

user5400206
user5400206

Reputation:

You can use the shadow XML attributes.

android:shadowColor="#000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="50"

For more info check this link, https://developer.android.com/reference/android/widget/TextView.html#attr_android:shadowColor

Upvotes: 1

Subrata Mondal
Subrata Mondal

Reputation: 852

I assume that you want to put the shadow on the TextView and not on the text itself. The you can make a layer-list inside your rounded corner drawable file like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape  android:shape="rectangle">
            <solid android:color="@android:color/black" />
            <corners android:radius="3dp" />
        </shape>
    </item>
    <item android:bottom="2dp" android:right="2dp">
        <shape  android:shape="rectangle">
            <solid android:color="#282828" />
            <stroke android:width="1dp" android:color="#302f2f" />
        </shape>
    </item>
</layer-list>

If you put the drawable as background of your TextView

<TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/rounded_corner"

the output will link like this:enter image description here

Upvotes: 5

Brenddon Anjos
Brenddon Anjos

Reputation: 157

Just pu this on your TextView XML for shadow on the text:

android:shadowColor="@android:color/holo_purple"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="5"

you can edit with your preferences. And create a Drawable file for shadow in all input, more or less like this one:

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

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

    </shape>
</item>

<!-- White Top color -->
<item android:bottom="4px" android:left="0px" android:right="4px" android:top="0px">
    <shape android:shape="rectangle" >
        <solid android:color="#FFFFFF" />

    </shape>
</item>

</layer-list>

Upvotes: 1

Related Questions