Android "elevation" Not Showing A Shadow


Answer :

I've been playing around with shadows on Lollipop for a bit and this is what I've found:

  1. It appears that a parent ViewGroup's bounds cutoff the shadow of its children for some reason; and
  2. shadows set with android:elevation are cutoff by the View's bounds, not the bounds extended through the margin;
  3. the right way to get a child view to show shadow is to set padding on the parent and set android:clipToPadding="false" on that parent.

Here's my suggestion to you based on what I know:

  1. Set your top-level RelativeLayout to have padding equal to the margins you've set on the relative layout that you want to show shadow;
  2. set android:clipToPadding="false" on the same RelativeLayout;
  3. Remove the margin from the RelativeLayout that also has elevation set;
  4. [EDIT] you may also need to set a non-transparent background color on the child layout that needs elevation.

At the end of the day, your top-level relative layout should look like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     style="@style/block"     android:gravity="center"     android:layout_gravity="center"     android:background="@color/lightgray"     android:paddingLeft="40dp"     android:paddingRight="40dp"     android:paddingTop="20dp"     android:paddingBottom="20dp"     android:clipToPadding="false"     > 

The interior relative layout should look like this:

<RelativeLayout     android:layout_width="300dp"     android:layout_height="300dp"     android:background="[some non-transparent color]"     android:elevation="30dp"     > 

If you have a view with no background this line will help you

android:outlineProvider="bounds" 

By default shadow is determined by view's background, so if there is no background, there will be no shadow also.


Apparently, you cannot just set an elevation on a View and have it appear. You also need to specify a background.

The following lines added to my LinearLayout finally showed a shadow:

android:background="@android:color/white" android:elevation="10dp" 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?