Android "elevation" not showing a shadow
I have a ListView, and with each list item I want it to show a shadow beneath it. I am using Android Lollipop's new elevation feature to set a Z on the View that I want to cast a shadow, and am already doing this effectively with the ActionBar (technically a Toolbar in Lollipop). I am using Lollipop's elevation, but for some reason it isn't showing a shadow under the list items. Here is how each list item's layout is set up:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" style="@style/block" android:gravity="center" android:layout_gravity="center" android:background="@color/lightgray" > <RelativeLayout android:layout_width="300dp" android:layout_height="300dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:elevation="30dp" > <ImageView android:id="@+id/documentImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/alphared" android:layout_alignParentBottom="true" > <appuccino.simplyscan.Extra.CustomTextView android:id="@+id/documentName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" app:typeface="light" android:paddingLeft="16dp" android:paddingTop="8dp" android:paddingBottom="4dp" android:singleLine="true" android:text="New Document" android:textSize="27sp"/> <appuccino.simplyscan.Extra.CustomTextView android:id="@+id/documentPageCount" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" app:typeface="italic" android:paddingLeft="16dp" android:layout_marginBottom="16dp" android:text="1 page" android:textSize="20sp"/> </LinearLayout> </RelativeLayout> </RelativeLayout>
However, here is how it shows the list item, without a shadow:
I have also tried the following to no avail:
- Set the elevation to the ImageView and TextViews themselves instead of the parent layout.
- Applied a background to the ImageView.
- Used TranslationZ in place of Elevation.
I've been playing around with shadows on Lollipop for a bit and this is what I've found:
- It appears that a parent
ViewGroup's bounds cutoff the shadow of its children for some reason; and
- shadows set with
android:elevationare cutoff by the
View's bounds, not the bounds extended through the margin;
- 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:
- Set your top-level
RelativeLayoutto have padding equal to the margins you've set on the relative layout that you want to show shadow;
android:clipToPadding="false"on the same
- Remove the margin from the
RelativeLayoutthat also has elevation set;
- [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" >
How to use your Android or iOS device as a webcam, Possible Android 10 flaw causes Google Pixel, other phones to randomly lock up Buy The Latest Android Phones At A Low Out-Of-Pocket Cost At T-Mobile®
If you have a view with no background this line will help you
By default shadow is determined by view's background, so if there is no background, there will be no shadow also.
Possible Android 10 flaw causes Google Pixel, other phones to randomly lock up, Global Android 10 rollout for the Mi A2 Lite continues Stay protected and in control. Google Play Protect, regular security updates and control over how your data is shared. We’re dedicated to securing Android’s 2.5 billion+ active devices every day and keeping information private.
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:
Global Android 10 rollout for the Mi A2 Lite continues, The official site for Android app developers. Provides the Android SDK tools and API documentation. Android 8.1 update comes with many bug fixes, and small changes that add to the capabilities of the Android O. Android 8.1 features include activating the Pixel Visual Core for 3rd-party apps
one other thing that you should be aware of, shadows will not show if you have this line in the manifest:
I tried all of the suggested stuff but it only worked for me when i removed the line, the reason i had the line was because my app works with a number of bitmap images and they were causing the app to crash.
Phones & Tablets, Simplify your Android development, grow your user base, and monetize more effectively with Google services. Secure, up-to-date, and easy to use. Android One phones are certified to run the simplest, latest version of Android. Regular security updates every month. At least two years of OS upgrades. 2 And only loaded with the most essential apps.
If you have a view with no background this line will help you
If you have a view with background this line will help you
Android Developers, Get manufacturer help for your Android device. Samsung help site · LG help site · Motorola help site · Pixel help site · Xiaomi help site · Find how to get help from Android Carousel Explore Android, the world’s most popular mobile operating system. Use the previous and next buttons, as well as the keyboard arrows, to change the displayed item.
Android (operating system), You can find your device's Android version number, security update level, and Google Play system level in your Settings app. You'll get notifications when Android is an open-source mobile operating system based on a modified version of the Linux kernel and other open source software, designed primarily for touchscreen mobile devices such as smartphones and tablets.
Google APIs for Android, Your buyer's guide for the best Android phones, deals, news, and reviews! Android Central covers smartphones, watches, tablets, ChromeOS devices, Google VLC for Android is the best open source video and music player, fast and easy! SHAREit - Transfer & Share. SHAREit Technologies Co.Ltd.
Android Help, Android has new features across everything from your camera to your settings. There’s something for you. Live Caption With a single tap, Live Caption automatically captions videos, podcasts and audio messages—even stuff you record yourself. Without ever needing wifi or cell phone data.
- I tried your layout (without customviews) and shown the shadow in my phone, could you share a minimal project which reproduce the issue?
- possible duplicate of Android L FAB Button shadow
- Related question: stackoverflow.com/questions/26572048/…
- Thanks for this! I also found android:clipChildren="false" to be useful when applied to the parent relative layout.
- Glad to help @blaffie!
- It's so annoying that Google produces such shoddy code. We'll have to account for these framework bugs for years.
- Google only says (alas) : "Shadows are drawn by the parent of the elevated view, and thus subject to standard view clipping, clipped by the parent by default.".
- In my case it was the transparent color. Ok, transparent things don't cast shadows... haha
- What a strange thing, Preview showed elevation but real device didn't. Thanks, this one fixed the issue.
- Works for API level 21 and higher
- This sort of worked for me, but gave a diagonal border on the sides when applied to a textview. Setting the background to a color (same as parents background color) worked better.