45

I want to apply shadow to the ImageView. When I'm applying shadow to a TextView I'm getting it but same it's not getting to ImageView. How can I solve this problem?

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
  • I may be wrong, but what you are using is a "text shadow" on `TextView`. This is fine. What you asking for is a "box shadow" on `ImageView`. I don't believe this exists. You'd need to build a custom background, that looked like a shadow to achieve this. – Matthew Rudy Dec 02 '11 at 08:07
  • 3
    possible duplicate of [Custom ImageView with drop shadow](http://stackoverflow.com/questions/3693234/custom-imageview-with-drop-shadow) – Sergey Glotov Mar 21 '12 at 12:14
  • 1
    I think you are looking for this: http://stackoverflow.com/questions/3693234/custom-imageview-with-drop-shadow I hope this should help you. – Rohit Mandiwal Dec 02 '11 at 08:06
  • `android:elevation="2dp"` is good enough sometimes – ekashking May 30 '20 at 23:43
  • 2
    Seems like `android:elevation` works only if you have `android:background` defined. Without defining both I get no shadow at all. – Idrizi.A Nov 09 '20 at 14:56

7 Answers7

9

We can also use CardView which provides a rounded corner background and shadow. To use that you need to add the v7 CardView library as a dependency to the project in the build.gradle like below.

dependencies {
    compile 'com.android.support:cardview-v7:23.0.1'
    -------
}

Note: Change 23.0.1 in the above line with the respected version.

So I surrounded the ImageView with CardView to make shadow like below.

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@android:color/white">

    <ImageView
        android:id="@+id/dish_image"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:adjustViewBounds="true" />

</android.support.v7.widget.CardView>

It'll add a shadow around the image.

Note: I don't know whether it is a good workaround. I am a beginner. I tried to implement the CardView which gives an idea to implement the same for this. If it is not good please update me with the reason.

Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
3

This is taken from Romain Guy's presentation at Devoxx, pdf found here.

Paint mShadow = new Paint(); 
// radius=10, y-offset=2, color=black 
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000); 
// in onDraw(Canvas) 
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);

Hope this helps.

NOTES

Don't forget for Honeycomb and above you need to invoke setLayerType(LAYER_TYPE_SOFTWARE, mShadow), otherwise you will not see your shadow! (@Dmitriy_Boichenko)

SetShadowLayer does not work with hardware acceleration unfortunately so it greatly reduces performances (@Matt Wear)

Answer Taken from Here

For Api greater than 21. You can try in xml in imageview or Button : Read here at developer site

android:elevation="5dp"
Community
  • 1
  • 1
Avinash Verma
  • 2,572
  • 1
  • 18
  • 22
3

Create a file shadow_rect.xml

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

</layer-list>

And the use this as android:background="@drawable/shadow_rect within your Imageview.

Harry
  • 369
  • 2
  • 17
2

ImageView

  <ImageView
     ......
  android:elevation="2dp"
  android:background="@drawable/myrect"/>

The background drawable is defined as a rectangle with rounded corners:

<!-- res/drawable/myrect.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid android:color="#42000000" />
    <corners android:radius="5dp" />
</shape>
Suresh Suthar
  • 794
  • 8
  • 15
1

You can create a layer list drawable and put your two items (drawables) in it for an image and your shadow.

Your shadow item's position & content might change according to where you want to apply the shadow (top, left, right, both right & left etc) and style of your shadow.

stdout
  • 2,471
  • 2
  • 31
  • 40
0

you can use the proprety outlineSpotShadowColor

to put the shadow to true try setOutlineSpotShadowColor with @color/black and increase the elevation to 12dp

to put the shadow to false setOutlineSpotShadowColor with @android:color/transparent and decrease the elevation

0

CardView is the perfect solution for this but if you don't want to use then you can use the XML layer. Here I am applying shadow 4th side of the image.

drawable/bg_shadow.xml: If you need more width border then you can change/remove the value of android:right android:left android:top android:bottom

<?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="#e5e5e5" />
            <corners android:radius="4dp"/>
        </shape>
    </item>
    <item android:right=".5dp" android:left=".5dp" android:top=".5dp" android:bottom=".5dp">
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="4dp"/>
        </shape>
    </item>

</layer-list>

On the layout, you have to use it as a background, and android:elevation will give a light style. To avoid crop you can use android:layout_margin.

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center"
        android:elevation="2dp"
        android:layout_margin="2dp"
        android:background="@drawable/bg_shadow"
        android:src="@drawable/ic_image" />
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60