ess.crazy
ess.crazy

Reputation: 296

Shadow for custom button layout-Android

This is my Button.xml file:

<Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:id="@+id/tv4"

            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_below="@id/tv3"
            android:textStyle="bold"
            android:text="Contact"
            android:background="@drawable/custom_button"
            android:textSize="24sp"
            android:typeface="serif"
            android:layout_marginTop="2dip"
            android:layout_marginBottom="2dip"/>

This is my custom_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>        
    <shape>
        <gradient
            android:startColor="#48b7f4"
             android:endColor="#48b7f4"
            android:angle="270" />
        <!--stroke
            android:width="1dp"
            android:color="#000000" /-->
        <corners
            android:radius="6dp" />
    </shape>
</item></selector>

How do I add a shadow effect to my button? I have tried shadowcolor option in xml but it is not working. I have 4 such buttons in the same layout. Thanks in advance

Upvotes: 2

Views: 1050

Answers (1)

Mahesh Paul
Mahesh Paul

Reputation: 49

You could use layer-list resource to add multiple xml files like stack. shadow_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:drawable="@drawable/shadow"/>
   <item
     android:drawable="@drawable/custom_button"
     android:bottom="4px" 
     android:right="4px"/>  
</layer-list>

The shadow.xml

<item>        
    <shape>
        <gradient
            android:startColor="#000000"
            android:endColor="#000000"
            android:angle="270" />
        <corners
            android:radius="6dp" />
    </shape>
 </item>
</selector>

You could provide colors in the gradient as you wish. Set shadow.xml as your buttons background.

Upvotes: 1

Related Questions