how to add shadow effect on drawable in android
Asked Answered
H

3

33

enter image description hereI have created chat bubble with drawable which looks fine, now I want to add shadow effect below the drawable so it make 3d effect.I don't want to use 9-pitch image. I just want to know how i can add shadow effect on this drawable. my code is

----right_bubble_chat_drawable

 <?xml version="1.0" encoding="utf-8"?>
 <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="15dp" />

        <solid android:color="@color/chatrightbubbleColor" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

        <size
            android:height="@dimen/normal_button_height"
            android:width="@dimen/normal_button_width" />

--- for corner pointer 'chat_laftarraow'

  <?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
     <rotate
        android:fromDegrees="90"
        android:toDegrees="-90"
        android:pivotX="50%"
        android:pivotY="50%" >
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="-40%"
        android:pivotY="86%" >
        <shape
            android:shape="rectangle"  >
            <stroke android:color="#00aaef" android:width="1dp"/>
            <solid
                android:color="#00aaef"  />

        </shape>
       </rotate>
       </rotate>
      </item>
    </layer-list>

------- I am using them like

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
   <View
    android:id="@+id/left_chatArror"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="6dp"
    android:background="@drawable/chat_laftarraow"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_chat_drawable">

    </RelativeLayout>
  </RelativeLayout>

Please suggest me how can I add shadow effect below the bubble as in the image below

enter image description here

Hentrich answered 14/10, 2014 at 8:29 Comment(0)
A
40

You can try by implementing a layer-list that will act as the background for the LinearLayout and add your view inside this.

Quote from an answer to this question:

Add background_with_shadow.xml file to res/drawable. Containing:

<?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/darker_gray" />
        <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape 
            android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

Then add the the layer-list as background in your LinearLayout.

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>

EDIT

You can create seprate xml for creating gray image like thsis:

----right_bubble_shdw_chat_drawable

 <?xml version="1.0" encoding="utf-8"?>
 <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="15dp" />

        <solid android:color="@android:color/darker_gray" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

        <size
            android:height="@dimen/normal_button_height"
            android:width="@dimen/normal_button_width" />

--- for corner pointer 'chat_laftarraow_shdw'

  <?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
     <rotate
        android:fromDegrees="90"
        android:toDegrees="-90"
        android:pivotX="50%"
        android:pivotY="50%" >
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="-40%"
        android:pivotY="86%" >
        <shape
            android:shape="rectangle"  >
            <stroke android:color="@android:color/darker_gray" android:width="1dp"/>
            <solid
                android:color="@android:color/darker_gray"  />

        </shape>
       </rotate>
       </rotate>
      </item>
    </layer-list>

------- I am using them like

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
   <View
    android:id="@+id/left_chatArror"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="6dp"
    android:background="@drawable/chat_laftarraow"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_chat_drawable">

    </RelativeLayout>


   <View
    android:id="@+id/left_chatArrorShdw"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="15dp"
    android:background="@drawable/chat_laftarraow_shdw"/>

<RelativeLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxWidth="220dp"
    android:layout_marginRight="-3dp"
    android:orientation="horizontal"
    android:layout_toLeftOf="@+id/left_chatArror"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/right_bubble_shdw_chat_drawable">
  </RelativeLayout>
Annmaria answered 14/10, 2014 at 8:36 Comment(5)
make differnt layer-list code and just add <solid android:color="@android:color/darker_gray" /> inside of your color codeAnnmaria
I didn't get it, can you give some example or code.Hentrich
In your right_bubble_chat_drawable xml instead of <solid android:color="@color/chatrightbubbleColor" /> use <solid android:color="@android:color/darker_gray" /> and same as in ` 'chat_laftarraow'` where ever you are using android:color seprate xml for shadow and add another use same as for your bubbleAnnmaria
The top part (above the EDIT text) is a copy of this answer: stackoverflow.com/a/14871171Synoptic
can you help me out here https://mcmap.net/q/453157/-how-to-draw-a-dot-circle-inside-a-square-drawable-in-androidPhototelegraphy
G
9

I am using this drawable/shadow.xml file

<!-- Drop Shadow Stack -->
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#00CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#10CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>
<item>
    <shape>
        <padding
            android:bottom="1dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp" />

        <solid android:color="#20CCCCCC" />

        <corners android:radius="3dp" />
    </shape>
</item>

<!-- Background -->
<item>
    <shape>
        <solid android:color="@android:color/white" />

        <corners android:radius="3dp" />
    </shape>
</item>

Note :- Root layout should be layer-list. Inside it add items.

Gerhard answered 3/7, 2019 at 5:40 Comment(0)
H
2

Add round_big_green_down.xml as background to your position.

<LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/**round_big_green_down**"
       android:paddingBottom="@dimen/button_margin_100"/>

res/drawable/round_big_green_down.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Drop Shadow Stack -->
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#00000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#01000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#02000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#03000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#04000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#05000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1.5dp" />
            <solid android:color="#06000000" />
            <corners android:radius="@dimen/button_radius" />
        </shape>
    </item>

    <!-- White Top color -->
    <item android:top="@dimen/button_margin_5">
        <shape >
            <gradient
                android:angle="90"
                android:endColor="@color/primary_color_50"
                android:startColor="@color/primary_color_500" />
            <corners
                android:topLeftRadius="@dimen/button_radius"
                android:topRightRadius="@dimen/button_radius"/>
        </shape>
    </item>
</layer-list>

res/values/dimens/

<dimen name="button_radius">25dp</dimen>
Hymnody answered 17/6, 2020 at 17:29 Comment(1)
Thank for your concern. If I have any suggestion, will update.Hymnody

© 2022 - 2024 — McMap. All rights reserved.