Elevation for ImageView is not working. I declared ImageView in XML like this:
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="50dp"
android:elevation="10dp"
android:src="@drawable/youtube" />
What else should I do for elevation to work properly for ImageView?
The elevation shadow is derived from the background drawable of a View. If your ImageView has no background, you'll see no shadow.
If you want to change that behavior, you need to build your own
ViewOutlineProvider
and callView.setOutlineProvider()
to set it (and this is not trivial).On
Lollipop
it works setting elevation. Forpre-Lollipop,
you need to implement shadows by yourself.Support-v4
hasViewCompat.setElevation(view, value)
method for by if you check the sources there is no implementation. (at least, at the time of writing).Works in all latest versions
Use CardView as parent layout. Use
android:background="@drawable/your_img"
instead ofandroid:src="@drawable/your_img"
in ImageView.If you are using CardView, be sure to leave the CardView background transparent.
Instead of using android:src you need to change to android:background for displaying shadows.
For displaying a round Image with SHADOW with elevation attribute, I created a round drawable WITHOUT SHADOW as shown below :
Use this drawable as background in your ImageView as shown below :
This will display ImageView with shadow.
One question that might be arising in your mind is why to use xml drawable. As mentioned by other developers on various answers to this question, shadow is formed from background and a bordered background to be more specific. Xml drawable helps us in creating a bordered background from which elevation attribute can create shadow.