Search code examples
androidtextviewshadow

How do I Set the Shadow around TextView


<TextView
            android:id="@+id/text"
            android:layout_width="225dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_toRightOf="@+id/left"
            android:background="@anim/chatitemshaodow"
            android:fontFamily="calibri"
            android:padding="8dp"
            android:textColor="#636363"
            android:textSize="17dp" />

This is my TextView controls and I want set the shadow around TextView. I am able to set the Showdown on text in TextView but I am unable to set shadow round on TextView.

I want to set textview shadow like given Screen in android. enter image description here


Solution

  • 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