Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add shadow to custom shape on Android

Is it possible to add a drop shadow to a custom shape in Android? After looking through the documentation, I only see a way to apply a text shadow.

I've tried this with no luck:

<?xml version="1.0" encoding="UTF-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">       <solid android:color="#90ffffff"/>        <corners android:radius="12dp" />      <item name="android:shadowColor">#000000</item>       <item name="android:shadowRadius">5</item>       <item name="android:shadowDy">3</item>     </shape> 
like image 424
mdunkle Avatar asked Mar 04 '11 18:03

mdunkle


2 Answers

After Lots of search finally I got this

enter image description here

<?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" />         <corners android:radius="7dp" />      </shape> </item>  <!-- White Top color --> <item android:bottom="3px">      <shape  android:shape="rectangle">      <solid android:color="#FFFFFF" />     <corners android:radius="7dp" />       </shape>  </item>   </layer-list> 
like image 63
Chirag Patel Avatar answered Oct 13 '22 00:10

Chirag Patel


This is how I do it:

Android Native Button with Shadows

Code bellow for one button STATE:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >     <!-- "background shadow" -->     <item>         <shape android:shape="rectangle" >             <solid android:color="#000000" />              <corners android:radius="15dp" />         </shape>     </item>     <!-- background color -->     <item         android:bottom="3px"         android:left="3px"         android:right="3px"         android:top="3px">         <shape android:shape="rectangle" >             <solid android:color="#cc2b2b" />               <corners android:radius="8dp" />         </shape>     </item>     <!-- over left shadow -->     <item>         <shape android:shape="rectangle" >             <gradient                 android:angle="180"                 android:centerColor="#00FF0000"                 android:centerX="0.9"                 android:endColor="#99000000"                 android:startColor="#00FF0000" />              <corners android:radius="8dp" />         </shape>     </item>     <!-- over right shadow -->     <item>         <shape android:shape="rectangle" >             <gradient                 android:angle="360"                 android:centerColor="#00FF0000"                 android:centerX="0.9"                 android:endColor="#99000000"                 android:startColor="#00FF0000" />              <corners android:radius="8dp" />         </shape>     </item>     <!-- over top shadow -->     <item>         <shape android:shape="rectangle" >             <gradient                 android:angle="-90"                 android:centerColor="#00FF0000"                 android:centerY="0.9"                 android:endColor="#00FF0000"                 android:startColor="#99000000"                 android:type="linear" />              <corners android:radius="8dp" />         </shape>     </item>     <!-- over bottom shadow -->     <item>         <shape android:shape="rectangle" >             <gradient                 android:angle="90"                 android:centerColor="#00FF0000"                 android:centerY="0.9"                 android:endColor="#00FF0000"                 android:startColor="#99000000"                 android:type="linear" />              <corners android:radius="8dp" />         </shape>     </item> </layer-list> 

Then you should have a selector with diferent versions of the button, something like:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">      <item android:drawable="@drawable/ic_button_red_pressed" android:state_pressed="true"/> <!-- pressed -->     <item android:drawable="@drawable/ic_button_red_selected" android:state_focused="true"/> <!-- focused -->     <item android:drawable="@drawable/ic_button_red_selected" android:state_selected="true"/> <!-- selected -->     <item android:drawable="@drawable/ic_button_red_default"/> <!-- default -->  </selector> 

hope this can help you..good luck

like image 42
João M Avatar answered Oct 13 '22 00:10

João M