I would like to create the same border of this LinearLayout as the example :
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>
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 result:
The source:
This is so simple:
Create a drawable file with a gradient like this:
for shadow below a view
below_shadow.xml
for shadow above a view
above_shadow.xml
and so on for right and left shadow just change the angle of the gradient :)
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.
2.now on the layout where you want the shadow(example: LinearLayout) add this in android:background
and that worked for me.
Try this..
I found the best way to tackle this.
You need to set a solid rectangle background on the layout.
Use this code -
ViewCompat.setElevation(view , value)
On the parent layout set
android:clipToPadding="false"
As an alternative, you might use a 9 patch image as the background for your layout, allowing for more "natural" shadows:
Result:
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/