Creating a frame for a view with gradient in xml - android

I would like to know how to create a kind of gradient frame for a background rectangle with xml.
It's simple to create a background gradient for 1 side with xml definition like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:type="linear"
android:centerX="8%"
android:startColor="#FF004D4D"
android:centerColor="#FF000000"
android:endColor="#FF000000"
android:angle="180"/>
</shape>
which results into a background like this:
But how can I create a similar gradient for all sides ? So that it would look something like this (apologies for 30 seconds photoshopping):
I tried to create a layer-list, as suggested in other questions regarding multiple gradients, but that didn't seem to work, it just took the "last item", and used those settings..
I am using setBackgroundResource(R.drawable.gradient_bg); in onDraw method for View if that is of any importance. Thanks for any suggestions!

The answer was indeed layered-list. The mistake was just not lowering the alpha on the endColor. So this works as expected:
<?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:type="linear"
android:centerX="8%"
android:startColor="#FFFF0000"
android:centerColor="#00FF0000"
android:endColor="#00000000"
android:angle="0"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:type="linear"
android:centerX="8%"
android:startColor="#FFFF0000"
android:centerColor="#00FF0000"
android:endColor="#00000000"
android:angle="90"/>
</shape>
</item>
</layer-list>

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

Re-use xml gradient in other xml drawable

I have a gradient defined in bg_gradient.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/colorPrimaryDark"
android:endColor="#color/colorPrimary"
android:type="linear"
android:angle="315"/>
</shape>
now I'd like to create a drawable xml which is supposed to be a circle filled with this gradient.
This is what I have so far:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="#dimen/size_update_indicator"
android:width="#dimen/size_update_indicator">
</size>
<gradient /* somehow I need to refer to bg_gradient here*/ ... />
</shape>
Is there a way I can refer to the existing gradient instead of just copy pasting it?

how to make gradient exactly like these drawable in android

NOTE : updated question !
i want to make gradiend as mentioned in the pic
the centered gray is a line i want it to be merged with both right left
Solution tried
Android drawable with background and gradient on the left
i couldnt find more about custom drawable gradient how i need
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:centerColor="#color/white"
android:endColor="#color/gray"
android:startColor="#color/gray"
android:type="linear"
/>
</shape>
i have these two images and want to make them with drawable ..
thanks in advance ..
PS : I have also tried the answer i got against my question but i think this gradient isnt possible i have been waiting since 3 months for solution.
Try to use type as radial :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<size
android:width="48dip"
android:height="48dip" />
<gradient
android:centerColor="#android:color/white"
android:centerY="0.5"
android:gradientRadius="180"
android:type="radial"
android:useLevel="false" />
</shape>
try this and change color what you want
android:background:"#drawable/color_grad"
color_grad.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#FAFAFA"
android:endColor="#C7C7C7"
android:startColor="#C7C7C7"
android:type="linear"
android:angle="270"/>
<corners android:radius="0dp" />
android:background="#drawable/gradient"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#C3C3C3"
android:endColor="#00000000"
android:angle="45"/>
</shape>

Android: How to set inner boarder shadow in a drawable?

I want to create an inner boarder shadow to a layout. However so far no luck. Currently I am testing a drawable but the effect is not as pleasant as expected.
Here is my attempt:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#000000"
android:endColor="#FFFFFF"
android:angle="0" />
</shape>
</item>
<item
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp">
<shape android:shape="rectangle">
<solid android:color="#70D159"/>
</shape>
</item>
I want to achieve something like this:
Is there a better approach to achieve this? Thank you in advance!

Creating a drawable rectangle in xml with one gradient on the top half and another on the bottom half

I'm trying to create a drawable in xml, a rectangle with one gradient on the top half, and another on the bottom half. This is NOT the way to do it, apparently:
<?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="#5a5a5a88"
android:endColor="#14141488"
android:angle="270" android:centerX="0.25"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" android:top="80px">
<gradient
android:startColor="#5aff5a88"
android:endColor="#14ff1488"
android:angle="270" android:centerX="0.25"/>
</shape>
</item>
</layer-list>
How can I do this, preferably in a way that makes it stretchable to any height?
If your goal is to have a gradient with a central color (starts at color A, transitions to B in the middle, then transitions to C at the end), add android:centerColor and android:centerY attributes to one of your <shape> elements and nuke the other. You can do the three-color gradient all in a single shape.
small bug for you, top attribute should be in item element ;)
it work:
<?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="#5a5a5a88"
android:endColor="#14141488"
android:angle="270" android:centerX="0.25"/>
</shape>
</item>
<item android:top="80dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#5aff5a88"
android:endColor="#14ff1488"
android:angle="270" android:centerX="0.25"/>
</shape>
</item>
</layer-list>

Categories

Resources