Android translucent background with shadow - android

I'm trying to create a background drawable resource in XML that is translucent, but with a solid shadow.
This is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<padding
android:top="-2dp"
android:left="-2dp"
android:right="2dp"
android:bottom="2dp"
/>
<gradient
android:startColor="#android:color/transparent"
android:centerColor="#33333333"
android:endColor="#aa666666"
android:angle="90"/>
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#99000000" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
I'm using a gradient shape as the background, which unfortunately obscures the translucency of the background shape. I only want to draw the gradient 2px to the right and bottom of the background shape, is that possible?

Try this...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right ="2dp"
android:bottom ="2dp">
<shape android:shape="rectangle" >
<padding
android:left="2dp"
android:top="2dp"
/>
<corners android:radius="3dp" />
<gradient
android:startColor="#android:color/transparent"
android:centerColor="#33333333"
android:endColor="#aa666666"
android:angle="90"/>
</shape>
</item>
<!-- Background -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#99000000" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>

Related

How to add inner shadow effect in drawable android?

I want to add inner shadow effect into my drawable file like this.
Below is my drawable file. I want to achieve shadow inside the drawable file as you can see above. What changes should i make to achieve this?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:bottom="#dimen/_3sdp"
android:left="#dimen/_8sdp"
android:right="#dimen/_8sdp"
android:top="#dimen/_3sdp">
<shape>
<corners android:radius="#dimen/_75sdp" />
<padding android:bottom="#dimen/_8sdp"
android:top="#dimen/_8sdp" />
<solid android:color="#color/leave_msg_color" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="#dimen/size_point5dp"
android:left="#dimen/size_point5dp"
android:right="#dimen/size_point5dp"
android:bottom="#dimen/size_point5dp">
<shape android:shape="rectangle">
<solid android:color="#color/blue_E7F1FF"/>
<corners android:radius="8dp"/>
</shape>
</item>
<!-- left shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="180"
android:centerColor="#00FF0000"
android:centerX="0.98"
android:endColor="#5C000000"
android:startColor="#00FF0000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- right shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="360"
android:centerColor="#00FF0000"
android:centerX="0.98"
android:endColor="#5C000000"
android:startColor="#00FF0000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- top shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:centerColor="#00FF0000"
android:centerY="0.1"
android:endColor="#00FF0000"
android:startColor="#6B000000" />
<corners android:radius="8dp" />
</shape>
</item>
<!-- bottom shadow -->
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="#00FF0000"
android:centerY="0.1"
android:endColor="#00FF0000"
android:startColor="#6B000000" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
The image would look like image
You can make many item inside your layer-list to produce shadow effect inside check this one out Add shadow to custom shape on Android.
You just need to change some values to achieve your wanted output. make the radius to get oval form and the values of colors.

Border Stroke with gradient colours and rounded corners [duplicate]

I want to create a border for a linearLayout. So I decide to create a shape. I want the border to have a gradient. The following is not doing it. It fills the rectangle and then creates a stroke. I don't want a filled rectangle, just the stroke. And I want to apply the gradient to the stroke.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<stroke
android:width="2dp"
android:color="#ff207d94" />
</shape>
try something like this:
<?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:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<stroke
android:width="2dp"
android:color="#ff207d94" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
since the accepted answer didn't work exactly as i wanted it to work for me, i'll post my solution too, maybe it helps someone else :)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<!-- create gradient you want to use with the angle you want to use -->
<shape android:shape="rectangle" >
<gradient
android:angle="0"
android:centerColor="#android:color/holo_blue_bright"
android:endColor="#android:color/holo_red_light"
android:startColor="#android:color/holo_green_light" />
</shape>
</item>
<!-- create the stroke for top, left, bottom and right with the dp you want -->
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<!-- fill the inside in the color you want (could also be a gradient again if you want to, just change solid to gradient and enter angle, start, maybe center, and end color) -->
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
This will create a layout with top border of 2dp.
just set it as a background to your layout
<?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="#4fc949"
android:centerColor="#0c87c5"
android:endColor="#b4ec51"
android:angle="180" />
</shape>
</item>
<item android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/background_color"/>
</shape>
</item>
</layer-list>
This would be the appropriate solution to what you wanna do. It includes gradient in stroke as well as a gradient in fill colour.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape >
<gradient
android:startColor="#2196F3"
android:endColor="#673AB7"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#color/transparentColor" />
<corners
android:radius="8dp" />
<padding
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
</shape>
</item>
<item android:top="0dp">
<shape>
<gradient
android:startColor="#FBB100"
android:endColor="#FF9900"
android:angle="270"/>
<corners
android:radius="8dp" />
</shape>
</item>
</layer-list>
This extra source should fix your problem
<?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:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<size android:height="170dp"
android:width="170dp"/>
</shape>
</item>
<item android:top="2dp" android:bottom="2dp" android:right="2dp" android:left="2dp">
<shape android:shape="rectangle">
<size android:width="140dp"
android:height="140dp"/>
<solid android:color="#color/colorAccent"/>
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>

How to add faded edges to a rectangle drawable?

I currently have the following drawable which creates a rectangle with rounded edges, a semi-transparent black background and transparent edges (in case I want to show them later):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#android:color/transparent" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#50FFFFFF"/>
<stroke
android:width="5dip"
android:color="#android:color/transparent" />
<corners android:radius="30dp" />
</shape>
</item>
</layer-list>
My question is how could I modify this so that the edges of the rectangle's background fade, to give a softer appearance?
I did something like this for work once.
Just make a layer-list with multiple rectangle each with increasing opacity and padding
code below:
<?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="#03101010" />
<corners android:radius="24dp" />
</shape>
</item>
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="rectangle">
<solid android:color="#06101010" />
<corners android:radius="20dp" />
</shape>
</item>
<item
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp">
<shape android:shape="rectangle">
<solid android:color="#09101010" />
<corners android:radius="16dp" />
</shape>
</item>
<item
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp">
<shape android:shape="rectangle">
<solid android:color="#12101010" />
<corners android:radius="12dp" />
</shape>
</item>
</layer-list>
The number of layers may vary depending on the color of your primary rectangle and the background.

How to create EditText background %100 transparent with bottom line

I need to create Edittext drawable background with %100 percent transparent and has only colored bottom line 2dp. Is there any way to make this component.
The bellow code with constant background. I need transparent background.
<?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="#color/colorPrimary2" />
<padding android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/background" />
</shape>
</item>
</layer-list>
Here you go:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" /> <!--background color -->
</shape>
</item>
<item
android:top="-3dp"
android:right="-3dp"
android:left="-3dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="2dp"
android:color="#000" />
<!-- color and size of the border -->
</shape>
</item>
</layer-list>
There is color for a transparency in android. Try this.
<?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="#FFffffff" />
<padding android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/background" />
</shape>
</item>
</layer-list>

Android shape border with gradient

I want to create a border for a linearLayout. So I decide to create a shape. I want the border to have a gradient. The following is not doing it. It fills the rectangle and then creates a stroke. I don't want a filled rectangle, just the stroke. And I want to apply the gradient to the stroke.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<stroke
android:width="2dp"
android:color="#ff207d94" />
</shape>
try something like this:
<?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:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<stroke
android:width="2dp"
android:color="#ff207d94" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
since the accepted answer didn't work exactly as i wanted it to work for me, i'll post my solution too, maybe it helps someone else :)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<!-- create gradient you want to use with the angle you want to use -->
<shape android:shape="rectangle" >
<gradient
android:angle="0"
android:centerColor="#android:color/holo_blue_bright"
android:endColor="#android:color/holo_red_light"
android:startColor="#android:color/holo_green_light" />
</shape>
</item>
<!-- create the stroke for top, left, bottom and right with the dp you want -->
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<!-- fill the inside in the color you want (could also be a gradient again if you want to, just change solid to gradient and enter angle, start, maybe center, and end color) -->
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
This will create a layout with top border of 2dp.
just set it as a background to your layout
<?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="#4fc949"
android:centerColor="#0c87c5"
android:endColor="#b4ec51"
android:angle="180" />
</shape>
</item>
<item android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/background_color"/>
</shape>
</item>
</layer-list>
This would be the appropriate solution to what you wanna do. It includes gradient in stroke as well as a gradient in fill colour.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape >
<gradient
android:startColor="#2196F3"
android:endColor="#673AB7"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#color/transparentColor" />
<corners
android:radius="8dp" />
<padding
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
</shape>
</item>
<item android:top="0dp">
<shape>
<gradient
android:startColor="#FBB100"
android:endColor="#FF9900"
android:angle="270"/>
<corners
android:radius="8dp" />
</shape>
</item>
</layer-list>
This extra source should fix your problem
<?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:angle="360"
android:centerColor="#e95a22"
android:endColor="#ff00b5"
android:gradientRadius="360"
android:startColor="#006386"
android:type="sweep" />
<size android:height="170dp"
android:width="170dp"/>
</shape>
</item>
<item android:top="2dp" android:bottom="2dp" android:right="2dp" android:left="2dp">
<shape android:shape="rectangle">
<size android:width="140dp"
android:height="140dp"/>
<solid android:color="#color/colorAccent"/>
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>

Categories

Resources