Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elevation not working for ImageView

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?

like image 974
Karthik Avatar asked Jun 04 '15 09:06

Karthik


People also ask

How do I set elevation in Android?

To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.

What is the use of elevation in Android?

Elevation (Android) Elevation is the relative depth, or distance, between two surfaces along the z-axis. Specifications: Elevation is measured in the same units as the x and y axes, typically in density-independent pixels (dp).

What is ImageView code?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling. To learn more about Drawables, see: Drawable Resources.


2 Answers

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 call View.setOutlineProvider() to set it (and this is not trivial).

like image 149
BladeCoder Avatar answered Sep 18 '22 18:09

BladeCoder


For displaying a round Image with SHADOW with elevation attribute, I created a round drawable WITHOUT SHADOW as shown below :

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >     <item>         <shape android:shape="oval">             <solid android:color="@android:color/white" />         </shape>     </item>  </layer-list> 

Use this drawable as background in your ImageView as shown below :

    <ImageView         android:id="@+id/my_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@drawable/round_shape"         android:elevation="5dp"         android:src="@drawable/center" /> 

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.

like image 20
Tarun Deep Attri Avatar answered Sep 20 '22 18:09

Tarun Deep Attri