Learn Pain Less

HomeOur TeamContact
Android
Add shadow using drawable without png android
Pawneshwer Gupta
Pawneshwer Gupta
January 16, 2017
1 min

Table Of Contents

01
Add shadow using drawable without png android
02
Below is code to make shadow effect with drawable file:
Add shadow using drawable without png android

Add shadow using drawable without png android

Shadow effect or Elevation will work on lollipop and higher versions of Android using xml property android:elevation=“4dp” and in java view.setElevation(4);. But these properties will not work on pre lollipop devices. Then to get shadow effect we can use 9 patch png images with shadow effect to show shadow below your view. But multiple png will take more storage space and make your app heavy.

In this case you can use xml drawables to make background for your views.

Below is code to make shadow effect with drawable file:

file name :- res/drawable/shadow_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<corners android:radius="3dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="4dp"/>
</shape>
</item>
</layer-list>

to use this background in your layout, just set this drawable as background on that layout and done. for example

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shadow_bg"/>

So in this code I used <Layer-list>, in layer list items show in stack. First item will be shown on top of all items.

I have given background color to my first item, then two second item I given grey color which will be shown as shadow. You can customize this code according to your need.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

shadowdrawable

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media