Gradiant Custom Shape in Android XML - android

I am looking for some tutorial or some reference which can help me to create some custom shapes like below-mentioned shape.Background Shape
I have tried creating a shape using the standard shape in android xml, but I am not able to find an appropriate solution for it.
It would be a good help if someone can explain how can I achieve this.

Try using this layer-list in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" android:tint="#0000ff">
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp"></padding>
</shape>
</item>
<item>
<bitmap
android:gravity="right"
android:src="Your_blur_backgound_image" />
// you can use any other image here
</item>

You should use a <layer-list>, 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="#ffffff"/>
<size android:height="30dp" android:width="40dp"/>
</shape>
</item>
<item>
<shape
android:shape="oval">
<gradient
android:type="radial"
android:gradientRadius="15dp"
android:endColor="#ffffff"
android:startColor="#0000ff" />
</shape>
</item>
<item>
<inset
android:insetLeft="5dp"
android:insetRight="15dp"
android:insetTop="5dp"
android:insetBottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
</inset>
</item>
</layer-list>
Result:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:centerX="0.1"
android:centerY="0.1"
android:centerColor="#1976d2"
android:startColor="#00e5ff"
android:endColor="#6200ea"
android:gradientRadius="100"
android:type="linear"/>
<padding android:left="5dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
<corners android:radius="6dp" />
</shape>

Related

How to draw a circular stroke inside a circle?

I'm trying to achieve this effect, but I can't make the white circular stroke inside the circle. In my implementation, the white stroke appears outside the circle
<?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="oval">
<corners android:radius="10dip" />
<stroke
android:width="5dip"
android:color="#ffffff" />
<solid android:color="#f50000" />
</shape>
</item>
</layer-list>
Expected output:-
I accomplished this using a <layer-list> and an <inset> oval shape:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#f50000"/>
</shape>
</item>
<item>
<inset
android:insetTop="5dp"
android:insetLeft="5dp"
android:insetRight="5dp"
android:insetBottom="5dp">
<shape android:shape="oval">
<stroke
android:color="#fff"
android:width="5dp"/>
</shape>
</inset>
</item>
</layer-list>
You can control the inset amount and the stroke width by changing the values in the second item
here you go. Just modify a little your file to have two items and add a padding on the second.
I hope it helps:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid android:color="#f50000" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape
android:shape="oval">
<stroke
android:width="2dp"
android:color="#ffffff" />
</shape>
</item>
</layer-list>
You need an xml like this (optimized):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid android:color="#007" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape
android:shape="oval">
<stroke
android:width="1dp"
android:color="#f00" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
</layer-list>
or xml like this (not optimized):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#007" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="8dp"
android:color="#android:color/transparent" />
<solid android:color="#f00" />
<size
android:width="45dp"
android:height="45dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="10dp"
android:color="#android:color/transparent" />
<solid android:color="#007" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
</layer-list>
for this result:

How to set up only circle size(in xml)?

I want to set up circle size, but I don't want resize other elements in layer list.
I have this code:
<?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/drawer_color" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:endColor="#1b2727"
android:centerColor="#1a2a29"
android:startColor="#154440"
android:type="linear"
android:angle="90"/>
<size
android:height="600dp"
android:width="600dp" />
<stroke
android:width="3dip"
android:color="#color/drawer_ring_color" />
</shape>
</item>
</layer-list>
What I found:
And what I want to see:
How to do that?
Thanks!
Unfortunately this do not works this way, your drawable will take the size of view where you use that drawable.
Try to set padding in your code.
<item>
<shape android:shape="rectangle">
<solid android:color="#color/drawer_color" />
<padding
android:left="10dp"
android:top="100dp"
android:right="10dp"
android:bottom="100dp" />
</shape>
</item>

How to change background color in shapes of android?

I need to change the background color of a button using shapes.
`
<?xml version="1.0" encoding="utf-8"?/>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_active="true">
<shape android:shape="rectangle">
<solid android:color="#color/blue3"/>
<stroke android:color="#color/white1" android:width="2dp" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
`
I am trying to set blue3 color using solid but it's not appearing in view. Please make me correct if i am trying to do it incorrect way.
Use this file in drawable
<?xml version="1.0" encoding="utf-8"?>
<item>
<layer-list >
<item>
<shape>
<solid android:color="#d4d4d4"></solid>
<corners android:radius="2dp"></corners>
</shape>
</item>
<item android:right="2dp" android:bottom="2dp" android:left="1dp" android:top="1dp">
<shape>
<solid android:color="#ff0000"></solid>
<corners android:radius="2dp"></corners>
</shape>
</item>
</layer-list>
</item>
and set Button background from drawable
Hope this will help!
Here is the solution of this question.
`
<?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/blue3" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
`

Layer list containing circle with outer stroke

I would like to create the following background with a layer-list:
So basically a circle which has an outer stroke border.
I created the following XML:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#fff"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke android:color="#ff" android:width="2dp"/>
<size
android:width="25dp"
android:height="25dp"/>
</shape>
</item>
</layer-list>
But this shows only one solid oval. Any help is appreciated!
Try This Change Color as per your requirement
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="60dp"
android:height="60dp"
android:top="15dp"
android:right="15dp"
android:bottom="15dp"
android:left="15dp">
<shape
android:shape="oval">
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape
android:shape="oval">
<stroke android:width="8dp"
android:color="#ffffff"/>
</shape>
</item>
</layer-list>
Try this .xml File
Tried to edit also you want to Exact
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
/>
<solid
android:color="#"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="100dp"
/>
<stroke
android:width="3dp"
android:color="#33779C"
/>
android:shape="oval">>
<solid android:color="#FFFFFF" />
<size android:height="25dp"
android:width="25dp"/>
</shape>
The Result shape Like This...

Shadow-like drawable, using "layer-list"

Background
I'm trying to make a (fake) shadow below a view, so that the layers would be vertical in this manner:
a line of color "#43000000" with a height of "1dp"
below the line, a gradient that starts from the top with color "#1A000000" and ends with color "#00000000", and is of a height of "8dp".
The problem
For some reason, I can't draw the line correctly.
No matter what I do, the line for some reason takes the whole space, while the gradient seems fine.
What I tried
I tried using "line" as the shape and also "rectangle". I also tried using "stroke" instead of "solid", tried adding padding and margins...
Here's the XML of the drawable file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<size android:height="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#1A000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Again, this is not the only thing I've tried. It's just my first attempt.
The question
How can I fix this XML ? what is causing it to happen?
Here's what I've found to be working:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="8dp">
<shape android:shape="rectangle" >
<size android:height="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#10000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Tried with this,may it fulfill your need with gradient
<?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="#e040fb"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#E1BEE7"/>
<corners android:radius="2dp" />
<gradient
android:angle="270"
android:endColor="#e1bee7"
android:startColor="#e040fb" />
</shape>
</item>
</layer-list>
You can change the heights in this and get what you want
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="10dp"
android:gravity="bottom">
<shape
android:shape="oval">
<solid android:color="#color/shadow_color" />
</shape>
</item>
<item android:right="20dp" android:left="20dp" android:bottom="12dp"
android:top="10dp">
<shape
android:shape="rectangle">
<solid android:color="#color/border_color"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:right="30dp" android:left="30dp" android:bottom="23dp"
android:top="20dp">
<shape
android:shape="rectangle">
<solid android:color="#color/base_color"/>
<corners android:radius="15dp"/>
</shape>
</item>
</layer-list>

Categories

Resources