How to give a custom angle to a gradient? - android

when creating a background with a gradient, it is possible to set the gradient in multiplications of 45
how can I set the gradient angle to 60 for instance?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="45"
android:startColor="#color/gradient_bottom"
android:endColor="#color/gradient_top"
android:type="linear" />
</shape>
</item>
</selector>

You can use like this.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#color/purple"
android:endColor="#color/pink"
android:angle="60" />
</shape>
If you use shape then it's working fine.

Related

How to add second gradient wave?

I added such gradient as a background for my screen
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="45"
android:startColor="#0019A8"
android:centerColor="#4C5CB1"
android:endColor="#0019A8"
android:type="linear" />
</shape>
there is a result that I got
So, at the middle I have one gradient stripe, what I actually need is - two stripes, first one closer to right left edge and second one to left bottom edge.
But, gradient give just 3 options for set color start, center, end.
So, question is - how to do it?
Use layer list.
<?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="45"
android:startColor="#0019A8"
android:centerColor="#4C5CB1"
android:endColor="#0019A8"
android:type="linear" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:type="linear"
android:gradientRadius="100"
android:startColor="#14B2FF"
android:endColor="#android:color/transparent"
android:centerX="0.5"
android:centerY="0.75"/>
</shape>
</item>
</layer-list>
Result

Android shape border with gradient and middle transparent

I need to create a round corner border, where border with gradient.enter link description here is working.But My page is using a image as background which is also with gradient. I need to show background image from middle of the filed. I want to create just like below image:
round_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="4dp"
android:color="#color/transparent" />
<gradient
android:startColor="#374533"
android:centerColor="#432727"
android:endColor="#222430"
android:angle="135"/>
<corners android:radius="10dp" />
</shape>
round_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#efe301"
android:centerColor="#7dc17b"
android:endColor="#01dae6"
android:angle="180"/>
<corners android:radius="10dp" />
result_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/round_border"/>
<item android:drawable="#drawable/round_background" />
</layer-list>
Here, you can set android:width="4dp" in round_background.xml to set size of your border. use result result_drawable.xml where you want..
Enjoy. :}

XML Circle with gradient fill Android

I am trying to make an XML Button, circle shape, which is filled with a gradient color. This color needs to be changed programatically.
Here is an example of what I am trying to achieve.
My attempts to build this have ended up with a gradient background and a solid colored circle. Instead of the gradient circle and transparent background.
I am hoping to be able to use more then 2 colors or a radial gradient for the circle as well. Any help is greatly appreciated.
You can Try with making Drawable XML. Example
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="circle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
</shape>
Arrange angle based on your requirement.
If you wants to change Color progarmatically then read this:-
How do I programmatically set the background color gradient on a Custom Title Bar?
Belated but can be helpful for someone else
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="0" android:endColor="#fff" android:gradientRadius="#dimen/_30sdp" android:startColor="#color/colorPrimaryDark" android:type="radial" />
<size android:width="#dimen/_25sdp" android:height="#dimen/_25sdp" />
</shape>
</item>
Output
Late but someone might get helped. Thanks to #Faizan
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="270" android:endColor="#D10719" android:gradientRadius="120dp" android:startColor="#0A5A1B" android:type="linear" />
<size android:width="100dp" android:height="100dp" />
</shape>
</item>
</selector>
Output:

Layout with different colors

I'd like to have a background color of a RelativeLayout similar to that of an image shown in the link below. Ignore the black strip in the bottom half of the image.
I don't want to use the image as a background of the layout. Can you give me an idea of how to proceed?
You can use gradient. Set different gradient in selector as you want. Done!
GradientDrawable.class
This is what you want, set your colors. Enjoy!
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="30dp">
<shape android:shape="rectangle" >
<corners
android:topRightRadius="8dip"
android:topLeftRadius="8dip" />
<gradient
android:startColor="#030303"
android:endColor="#3B3B3B"
android:angle="270" />
</shape>
</item>
<item android:top="30dp" >
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip" />
<gradient
android:startColor="#4B5057"
android:endColor="#4B5059"
android:angle="270" />
<size android:height="30dp" />
</shape>
</item>
</layer-list>
just try with shape file structure. I think this may give the solution.
//save this below code as gradient and use as background of your layout as
android:background="#drawable/gradient"
//gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#232323" android:endColor="#4B5059"
android:angle="270"/>
</shape>

How to add padding to gradient <shape> in Android?

I have a shape with a gradient that I'm using as a divider between ListView items. I've defined it as follows:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ccd0d3"
android:centerColor="#b6babd"
android:endColor="#ccd0d3"
android:height="1px"
android:angle="0" />
</shape>
I would like to add 6 pixels of padding on either side of the gradient, so that it doesn't extend from edge to edge of the screen.
However, no matter where I put an android:left="6px" and android:right="6px", it doesn't seem to take effect. I can put it in the <shape> element, the <gradient> element, or in a separate <padding> child of <shape>, and it doesn't change anything.
How can I add padding on the left and right of my list divider?
I guess you could combine it like this:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="6dp"
android:right="6dp">
<shape android:shape="rectangle">
<gradient android:startColor="#ccd0d3"
android:centerColor="#b6babd"
android:endColor="#ccd0d3"
android:height="1px"
android:angle="0"/>
</shape>
</item>
</layer-list>
Another solution using inset:
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="6dp"
android:insetRight="6dp" >
<shape
android:shape="rectangle">
<gradient
android:startColor="#ccd0d3"
android:centerColor="#b6babd"
android:endColor="#ccd0d3"
android:height="1px"
android:angle="0" />
</shape>
</inset>
One solution seems to be to "wrap" my drawable with another drawable that specifies the appropriate padding.
For example, list_divider.xml would be:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="6dp"
android:right="6dp"
android:drawable="#drawable/list_divider_inner" />
</layer-list>
And then list_divider_inner.xml would be the original drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ccd0d3"
android:centerColor="#b6babd"
android:endColor="#ccd0d3"
android:height="1px"
android:angle="0" />
</shape>
This results in two files to specify a simple divider though. I don't know if there's a way to do it with only one file though.

Categories

Resources