Search code examples
androidandroid-layoutmaterial-designandroid-shape

Shadow Appearance on All Android Versions


In Lollipop we set a view's Shadow by setElevation() method.

But in pre-Lollipop, it doesn't work.

What is the best approach so that the View will have identical look when displayed on pre-lollipop devices and lollipop devices?


Solution

  • There's a bunch of approaches and each of them has its pros and cons:

    1. Draw shadows using generated gradients. Design Support Library does that. This approach supports dynamic shadows and is pretty fast. Doesn't fit into the drawing code well - usually you have to add it as a background and modify paddings.
    2. Draw shadows using 9patches. Better quality, probably faster than gradients. Doesn't support dynamic shadows and needs modified paddings and backgrounds as well.
    3. Draw shadows using RenderScript. The best quality, dynamic shadows and easy integration. It's the slowest approach and you need to setup RenderScript. Carbon draws shadows using RenderScript.