Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show shadow around the linearlayout in Android?

How can I show shadow for my linear layout. I want white colored rounded background with shadow around the linearlayout. I have done this so far.

<LinearLayout  android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@xml/rounded_rect_shape" android:orientation="vertical" android:padding="10dp"> <-- My buttons, textviews, Imageviews go here --> </LinearLayout> 

And rounded_rect_shape.xml under xml directory

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >     <solid android:color="#ffffff" />     <corners       android:bottomLeftRadius="3dp"       android:bottomRightRadius="3dp"       android:topLeftRadius="3dp"       android:topRightRadius="3dp" /> </shape> 
like image 944
Santhosh Avatar asked Oct 22 '12 06:10

Santhosh


People also ask

How do add shadow above a view in Android?

There is no such attribute in Android, to show a shadow. But possible ways to do it are: Add a plain LinearLayout with grey color, over which add your actual layout, with margin at the bottom and right equal to 1 or 2 dp.

What is LinearLayout in Android with example?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What is elevation in Android?

Elevation helps users understand the relative importance of each element and focus their attention to the task at hand. The elevation of a view, represented by the Z property, determines the visual appearance of its shadow: views with higher Z values cast larger, softer shadows.


2 Answers

There is also another solution to the problem by implementing a layer-list that will act as the background for the LinearLayoout.

Add background_with_shadow.xml file to res/drawable. Containing:

<?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="5dp"/>         </shape>     </item>     <item android:right="1dp" android:left="1dp" android:bottom="2dp">         <shape              android:shape="rectangle">         <solid android:color="@android:color/white"/>         <corners android:radius="5dp"/>         </shape>     </item> </layer-list> 

Then add the the layer-list as background in your LinearLayout.

<LinearLayout   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:background="@drawable/background_with_shadow"/> 
like image 80
muthee Avatar answered Sep 16 '22 23:09

muthee


Well, this is easy to achieve .

Just build a GradientDrawable that comes from black and goes to a transparent color, than use parent relationship to place your shape close to the View that you want to have a shadow, then you just have to give any values to height or width .

Here is an example, this file have to be created inside res/drawable , I name it as shadow.xml :

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">      <gradient         android:startColor="#9444"         android:endColor="#0000"         android:type="linear"         android:angle="90"> <!-- Change this value to have the correct shadow angle, must be multiple from 45 -->     </gradient>  </shape> 

Place the following code above from a LinearLayout , for example, set the android:layout_width and android:layout_height to fill_parent and 2.3dp, you'll have a nice shadow effect on your LinearLayout .

<View     android:id="@+id/shadow"     android:layout_width="fill_parent"     android:layout_height="2.3dp"       android:layout_above="@+id/id_from_your_LinearLayout"      android:background="@drawable/shadow"> </View> 

Note 1: If you increase android:layout_height more shadow will be shown .

Note 2: Use android:layout_above="@+id/id_from_your_LinearLayout" attribute if you are placing this code inside a RelativeLayout, otherwise ignore it.

Hope it help someone.

like image 42
Murillo Ferreira Avatar answered Sep 16 '22 23:09

Murillo Ferreira