Android rectangle background gradient - android

I want to crate a background gradient that goes from the inside to outside. And at the outside it should be getting darker. Here is an image:
Can anyone give me some advice how I can create something like that?
Or should I take an image for this? If yes, what's about different screen resolutions? Or about landscape / portrait modus?

use this change color as required
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="radial"
android:startColor="#f15330"
android:endColor="#da0000"
android:gradientRadius="150dp"
/>
<size android:height="400dp"
android:width="250dp"/>
</shape>

Yes , you can use nine patch image to achieve for this type gredient
but if you want use gredient then i think this will be help for you..
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="radial"
android:centerX="50%"
android:centerY="50%"
android:startColor="#FFff0000"
android:endColor="#FF000000"
android:gradientRadius="100"/>
</shape>
and this is olnline tool where you can create gredient and you can grab the code using android tab..

How you want is it like cardview?Or you can try with this also,
<?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>

Related

I want to give radius to shadow shown inside a layout in android

I want to give shadow inside a cardview and got solution like shown below :
but I also want to give radius to shadow as well which I'm not able to do,
Here's my code for the same :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/yellow"/>
<corners android:radius="#dimen/_20sdp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="180"
android:centerColor="#00FF0000"
android:centerX="0.97"
android:centerY="0.5"
android:endColor="#5C000000"
android:startColor="#00FF0000" />
</shape>
</item>
<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="#dimen/_20sdp" />
</shape>
</item>
</layer-list>
Can anyone help me with this?
Thanks in advance.
Take transfers image like a shadow you want the top layout.
or
create gradient drawable.
Try something like below
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>

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 center the gradient color in android

Hi I am trying to gradient in android.
I am trying the following code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:centerColor="#FFEBEE"
android:endColor="#EF5350"
android:angle="-270" />
</shape>
Result:
I want to increase the size of white in center. Please help me to solve my issue.
Try this :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="50dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:endColor="#EF5350"
android:centerColor="#FFFFFF"
android:angle="270"/>
<size
android:height="100dp"
android:width="80dp"/>
</shape>
</item>
<item
android:bottom="50dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:endColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:angle="270"
/>
<size
android:height="100dp"
android:width="80dp"/>
</shape>
</item>
</layer-list>
You need to specify type inside the gradient tag,
For e.g. in my case I have defined radial for center gradient effect,
android:type="radial"
I have created gradient shape (centergradietbg.xml) with center gradient effect, look it my code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:centerColor="#c1c1c1"
android:endColor="#4f4f4f"
android:gradientRadius="400"
android:startColor="#c1c1c1"
android:type="radial" >
</gradient>
</shape>
Use this xml in your layout background using
android:background="#drawable/centergradietbg"

Apply inner shadows for Multiline EditText android

I have a multiline EditText for recieving a query.
It looks like this
So far I have used the stroke but it isnt the same.
<?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="180"
android:centerColor="#color/white"
android:endColor="#color/white"
android:gradientRadius="360"
android:startColor="#color/white"
android:type="sweep" />
<stroke
android:width="0.5dp"
android:color="#color/light_gray" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="3dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="rectangle" >
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
what i achieved
The only thing missing is the gradient on all the sides of the corner.
I have referred few links here, here.
Anyone know the answer. Help. Thanks in advance.
You are using gradient,but all with white color,
<gradient
android:angle="180"
android:centerColor="#color/red"
android:endColor="#color/blue"
android:gradientRadius="360"
android:startColor="#color/white"
android:type="sweep" />
Make start, end and center color different and have a try.I didn't try it but it will work.

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