Android LinearLayout : Add border with shadow around a LinearLayout
Asked Answered
V

14

172

I would like to create the same border of this LinearLayout as the example :

enter image description here

In this example, we can see that the border is not the same all around the linearLayout. How can I create this using an XML drawable file?

For now, I have only able to create a simple border all around the LinearLayout like this :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <corners
      android:radius="1dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="#E3E3E1" />

  <solid android:color="@color/blanc" />

</shape>
Vortical answered 7/6, 2014 at 8:12 Comment(7)
you can use background property for that...create one XML file with shape like rectangle, color and shadow effect for it and set it as background for your linear layout..Lusatia
use stroke for gray border and padding effect tooLusatia
I think,that its two layout xml,say one is linear layout and inner is relative layout having paddingHeartwood
@Prag's Could you help me for creating this xml file please ?Vortical
An alternative way could be using a 9 patch image as the background for your layout. This would allow for a really smooth, fading shadow (much realistic, in my opinion).Electorate
See my answer to the same question over here https://mcmap.net/q/144792/-is-there-any-way-to-do-a-material-style-shadow-in-api-lt-21-5-0-lollipopImponderable
you can use carviewImpart
D
297

Try this, as shown in Android Developer Tips & Tricks by an anonymous contributor:

<?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="#CABBBBBB"/>
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>
Direful answered 7/6, 2014 at 8:24 Comment(0)
I
93

That's why CardView exists. CardView | Android Developers
It's just a FrameLayout that supports elevation in pre-lollipop devices.

<android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardUseCompatPadding="true"
    app:cardElevation="4dp"
    app:cardCornerRadius="3dp" >

    <!-- put whatever you want -->

</android.support.v7.widget.CardView>

To use this you need to add dependency to build.gradle:

compile 'com.android.support:cardview-v7:23.+'
Indecency answered 11/1, 2016 at 8:11 Comment(4)
how can change shadow colorArbour
shadow is very weak #49076688Search
very strange behavior in devices with 4.4.4 (or similar). A huge margin is added to cardview in and makes UI very ugly.Cervicitis
This should be the accepted answer. One small update though. You should use the androidx one -> androidx.cardview.widget.CardViewGoof
C
64

I get the best looking results by using a 9 patch graphic.

You can simply create an individual 9 patch graphic by using the following editor: http://inloop.github.io/shadow4android/

Example:

The 9 patch graphic:

The 9 patch graphic:

The result:

enter image description here

The source:

<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@drawable/my_nine_patch"
Chaste answered 30/7, 2015 at 11:9 Comment(2)
How can I hide the black line? Please helpDirected
@Isaac save the image as my_nine_patch.9.png (.9.png is 9 batch image)Shoring
A
42

okay, i know this is way too late. but i had the same requirement. i solved like this

1.First create a xml file (example: border_shadow.xml) in "drawable" folder and copy the below code into it.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<item>
    <shape>
        <!-- set the shadow color here -->
        <stroke
            android:width="2dp"
            android:color="#7000" />

        <!-- setting the thickness of shadow (positive value will give shadow on that side) -->

        <padding
            android:bottom="2dp"
            android:left="2dp"
            android:right="-1dp"
            android:top="-1dp" />

        <corners android:radius="3dp" />
    </shape>
</item>

<!-- Background -->

<item>
    <shape>
        <solid android:color="#fff" />
        <corners android:radius="3dp" />
    </shape>
</item>

</layer-list>

2.now on the layout where you want the shadow(example: LinearLayout) add this in android:background

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dip"
    android:background="@drawable/border_shadow"
    android:orientation="vertical">

and that worked for me.

Abaft answered 25/9, 2014 at 7:3 Comment(0)
C
28

This is so simple:

Create a drawable file with a gradient like this:

for shadow below a view below_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">   
<gradient
    android:startColor="#20000000"
    android:endColor="@android:color/transparent"
    android:angle="270" >
</gradient>
</shape>

for shadow above a view above_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">   
<gradient
    android:startColor="#20000000"
    android:endColor="@android:color/transparent"
    android:angle="90" >
</gradient>
</shape>

and so on for right and left shadow just change the angle of the gradient :)

Cervicitis answered 15/2, 2016 at 10:17 Comment(3)
where I call the below_shadow? background of a view?Manipur
Create below_shadow xml file, then in your view, add a "View" in your layout and set below_shadow.xml as background of this viewCervicitis
I did the same as you told, but my "above_shadow" is not transparent when i put that view in the custom view. It works fine separately. What should I do?Szechwan
B
19

As an alternative, you might use a 9 patch image as the background for your layout, allowing for more "natural" shadows:

enter image description here

Result:

enter image description here

Put the image in your /res/drawable folder.
Make sure the file extension is .9.png, not .png

By the way, this is a modified (reduced to the minimum square size) of an existing resource found in the API 19 sdk resources folder.
I left the red markers, since they don't seem to be harmful, as shown in the draw9patch tool.

[EDIT]

About 9 patches, in case you never had anything to do with them.

Simply add it as the background of your View.

The black-marked areas (left and top) will stretch (vertically, horizontally).
The black-marked areas (right, bottom) define the "content area" (where it's possible to add text or Views - you can call the unmarked regions "padding", if you like to).

Tutorial: http://radleymarx.com/blog/simple-guide-to-9-patch/

Borchardt answered 7/6, 2014 at 9:30 Comment(1)
Simply add it as the background of your View. The black-marked areas (left and top) will stretch (vertically, horizontally). The black-marked areas (right, bottom) define the "content area" (where it's possible to add text or Views - call it "padding", if you like to). Tutorial: radleymarx.com/blog/simple-guide-to-9-patchElectorate
W
10

You create a file .xml in drawable with name drop_shadow.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--<item android:state_pressed="true">
        <layer-list>
            <item android:left="4dp" android:top="4dp">
                <shape>
                    <solid android:color="#35000000" />
                    <corners android:radius="2dp"/>
                </shape>
            </item>
            ...
        </layer-list>
    </item>-->
    <item>
        <layer-list>
            <!-- SHADOW LAYER -->
            <!--<item android:top="4dp" android:left="4dp">
                <shape>
                    <solid android:color="#35000000" />
                    <corners android:radius="2dp" />
                </shape>
            </item>-->
            <!-- SHADOW LAYER -->
            <item>
                <shape>
                    <solid android:color="#35000000" />
                    <corners android:radius="2dp" />
                </shape>
            </item>
            <!-- CONTENT LAYER -->
            <item android:bottom="3dp" android:left="1dp" android:right="3dp" android:top="1dp">
                <shape>
                    <solid android:color="#ffffff" />
                    <corners android:radius="1dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

Then:

<LinearLayout
...
android:background="@drawable/drop_shadow"/>
Whoa answered 25/8, 2016 at 8:22 Comment(0)
S
6

1.First create a xml file name shadow.xml in "drawable" folder and copy the below code into it.

 <?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="#CABBBBBB" />
                <corners android:radius="10dp" />
            </shape>
        </item>

        <item
            android:bottom="6dp"
            android:left="0dp"
            android:right="6dp"
            android:top="0dp">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/white" />
                <corners android:radius="4dp" />
            </shape>
        </item>
    </layer-list>

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

<LinearLayout
        android:id="@+id/header_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shadow"
        android:orientation="vertical">
Sulphanilamide answered 14/1, 2020 at 9:51 Comment(1)
@Johndahat its okSulphanilamide
L
4

Use this single line and hopefully you will achieve the best result;

use: android:elevation="3dp" Adjust the size as much as you need and this is the best and simplest way to achieve the shadow like buttons and other default android shadows. Let me know if it worked!

Loveridge answered 7/10, 2018 at 10:5 Comment(1)
@MichałZiobro Use it with your layouts like if you have a layout over another which is smaller and you want to show the shadow. it should work. You may find your texts and buttons get invisible but when you will run the app you will see the best shadow. I'm not sure it's some kind of bug or something but in my case when I use that code it makes all my Views invisible in the AS preview but works best in the app when I debug/launch it in my device. Let me know if it worked or not. Thanks.Loveridge
R
3

If you already have the border from shape just add elevation:

<LinearLayout
    android:id="@+id/layout"
    ...
    android:elevation="2dp"
    android:background="@drawable/rectangle" />
Radioscope answered 4/2, 2019 at 23:35 Comment(2)
What is a behaviour on API < 21?Uncharted
Unfortunately it does not work pre Lollipop so you should use some of the other answers from this question. But if you're targeting newer phones this is a pretty easy way to do it that works well.Radioscope
R
3

I know this is late but it could help somebody.

You can use a constraintLayout and add the following property in the xml,

        android:elevation="4dp"
Remorseful answered 17/12, 2019 at 7:38 Comment(0)
A
2

Ya Mahdi aj---for RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#7d000000"
                android:endColor="@android:color/transparent"
                android:angle="90" >
            </gradient>
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="3dp"
        android:top="0dp"
        android:bottom="3dp">
        <shape android:shape="rectangle">
            <padding
                android:bottom="40dp"
                android:top="40dp"
                android:right="10dp"
                android:left="10dp"
                >
            </padding>
            <solid android:color="@color/Whitetransparent"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>
Amari answered 19/9, 2016 at 10:21 Comment(0)
C
1

I found the best way to tackle this.

  1. You need to set a solid rectangle background on the layout.

  2. Use this code - ViewCompat.setElevation(view , value)

  3. On the parent layout set android:clipToPadding="false"

Charliecharline answered 14/4, 2017 at 5:10 Comment(2)
In case you didn't know, this is the current implementation of ViewCompat.setElevation() method on the ViewCompat class: public void setElevation(View view, float elevation) {}. As you can see, it's not implemented. I really doubt it'll be of any help, and truly wonder if you actually tested your own answer, lolEssam
ViewCompat.setElevation(view, value) worked and solved my problem. I'm coding for min api level 16.Cripple
G
0

Just add this in your element : where android:translationZ="5dp" is most important

<LinearLayout 
android:background="@drawable/rounded_background"
android:translationZ="5dp"></LinearLayout>

@drawable/rounded_background.xml file code :

<?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="#fef8f0"/>
        <corners android:radius="10dp" />
    </shape>
</item>
</layer-list>
Gammy answered 15/4, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.