Android: writing drawables in xml - android

I need to create some drawables that look like image below, is there a way to do this in xml?
Edit:
I have got this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/darker_gray" />
<corners android:radius="6dp" />
</shape>
</item>
<item
android:bottom="3dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark_gray" />
<corners android:radius="6dp" />
</shape>
</item>
</layer-list>
it's the same like on the picture without that blue corner and I have no idea how to do that diagonal shape. Now I'm using 9 patch, but since all my other buttons are in xml, I was just wondering if there is some way to write this also in xml

Try this code, i guess this is what you want:
draw.xml
<?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:width="100dp"
android:height="60dp"/>
<stroke android:width="2dp" android:color="#333333" />
<solid android:color="#333333" />
<padding android:bottom="3dp"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<size android:width="100dp"
android:height="60dp"/>
<stroke android:width="2dp" android:color="#000000" />
<solid android:color="#000000" />
<padding android:bottom="1dp"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="-45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<size android:width="50dp"
android:height="50dp"/>
<stroke android:color="#android:color/transparent" android:width="0dp"/>
<solid
android:color="#android:color/holo_blue_light" />
<corners android:radius="5dp"/>
</shape>
</rotate>
</item>
<item>
<shape
android:shape="rectangle">
<size android:width="100dp"
android:height="60dp"/>
<stroke android:width="2dp" android:color="#android:color/transparent" />
<solid android:color="#android:color/transparent" />
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
and in your layout file
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:background="#drawable/draw" >
</RelativeLayout>

You should use layer list to combine multiple drawables below is an example.
You should also check this very complete guide to android drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#585858" />
<solid android:color="#FF9009" />
<padding android:bottom="1dp"/>
</shape>
</item>
<item android:left="10dp" android:top="20dp" android:bottom="20dp" android:right="150dp">
<shape
android:shape="oval">
<stroke android:width="1dp" android:color="#ffffff" />
<solid android:color="#118C4E" />
<padding android:bottom="1dp"/>
</shape>
</item>
<item android:left="150dp" android:top="20dp" android:bottom="20dp" android:right="10dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#ffffff" />
<solid android:color="#C1E1A6" />
<padding android:bottom="1dp"/>
</shape>
</item>
</layer-list>

Related

Not all items in layer-list are showing up

I'm trying to implement a view like what's in this link here: https://imgur.com/a/6b75AMp
This is the layer-list I'm using:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesLightGreen"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesBackground"/>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
// The width and color of the border
<stroke
android:width="1dp"
android:color="#3C341F" />
// The desired corner radius. reduce it to keep it less rounded
<corners android:radius="10dp" />
<solid
android:color="#F0ECE0"
/>
</shape>
</item>
</layer-list>
However, the border doesn't show up. If I play around with the order of the items--for example, if I make the vertical stripe the last item--then the border might show up, but the vertical stripe won't. How do I get everything in the layer-list to show up properly?
try the following code you will get a hint
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesLightGreen" />
</shape>
</item>
<item android:left="4dp" android:top="4dp" android:right="4dp" android:bottom="4dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/HobbesBackground" />
</shape>
</item>
<item android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp" android:color="#3C341F" />
<corners android:radius="10dp" />
<solid android:color="#F0ECE0" />
</shape>
</item>
I have modified some of your code, use it and chech the output.
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke android:color="#000000" android:width="2dp"/>
</shape>
</item>

How can I draw this border?

How can I draw this gradient border(border is fade out to transparent) and radial gradient background?
My code :
Drawable/bg.xml
Radial gradient code is here
Drawable/border.xml
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_blue_light"/>
<corners android:topLeftRadius="100dp" android:topRightRadius="100dp"/>
</shape>
</item>
<item android:left="3dp" android:right="3dp" android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
<corners android:topLeftRadius="100dp" android:topRightRadius="100dp"/>
</shape>
</item>
<item android:height="5dp" android:left="30dp" android:right="30dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<corners android:topLeftRadius="100dp" android:topRightRadius="200dp"/>
<gradient android:endColor="#color/transparent"
android:centerColor="#color/white"
android:startColor="#color/transparent"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient android:endColor="#color/white"
android:startColor="#android:color/transparent"
android:angle="-90" />
</shape>
</item>
I want this result :
<?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="#android:color/holo_blue_light"/>
<corners android:topLeftRadius="100dp" android:topRightRadius="100dp"/>
</shape>
</item>
<item android:left="3dp" android:right="3dp" android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
<corners android:topLeftRadius="100dp" android:topRightRadius="100dp"/>
</shape>
</item>
<item android:width="40dp" android:left="70dp" android:right="200dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<gradient android:endColor="#color/white"
android:startColor="#color/transparent"/>
</shape>
</item>
<item android:width="40dp" android:left="240dp" android:right="20dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
<gradient android:endColor="#color/transparent"
android:startColor="#color/white"/>
</shape>
</item>
<item android:left="110dp" android:right="120dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient android:endColor="#color/white"
android:startColor="#color/transparent"
android:angle="-90" />
</shape>
</item>
</layer-list>
Create this rectangle.xml file inside the drawable folder.
Copy this code into it.
<?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="270"
android:endColor="#000000"
android:startColor="#FFFFFF" />
<corners android:radius="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#af25e1" />
<corners android:radius="10dp" />
<size
android:width="250dp"
android:height="150dp" />
</shape>
</item>
</layer-list>
and set it as the background to your container.
You can change it according to your needs
Hope it works for you.

Custom shape in android using xml

I am trying to draw a custom shape which I can use as a background for my layout. But I am not able to do so. Is it possible to draw a shape as below using xml in android. I am not getting how to cut the semicircle shape from the vertical centres of rectangle.
Use layer-list to make this custom shape drawable.
/res/drawable/custom_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp" />
<size
android:width="300dp"
android:height="100dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<!-- Left Half-Circle -->
<item
android:left="-10dp"
android:right="290dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Right Half-Circle -->
<item
android:left="290dp"
android:right="-10dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Middle Dotted Line -->
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"
android:color="#ff00"/>
</shape>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
OUTPUT:
Hope this will help~
In your case it might be worth to create a resizable bitmap (9-Patch drawable). Follow this guide.
try bellow xml. save it in drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<solid
android:color="#ffffff"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#000000"
/>
</shape>
It is better to use 9-Patch images. If you can create an image like above you can use https://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create 9-patch image(define expandable area).
Try this xml file in drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="#color/primary" />
<corners android:radius="50dp"/>
</shape>
Please refer the below xml, it may help you out
<?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="#android:color/black" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="200dp"
android:height="200dp" />
<solid android:color="#color/colorAccent" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="line">
<solid android:color="#android:color/black" />
<stroke android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"/>
</shape>
</item>
<item
android:bottom="103dp"
android:left="-10dp"
android:right="205dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
<item
android:bottom="103dp"
android:left="205dp"
android:right="-10dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
</layer-list>
Try below code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="3dip" />
<stroke
android:width="1dp"
android:color="#ED2A3C" />
</shape>

Widths of layered rectangles do not behave as expected

I am trying to achieve the above background with the following 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="#dddddd" />
<size android:width="0dp" android:height="0dp" />
<stroke
android:width="3dp"
android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
</layer-list>
However, setting rectangle widths does not seem to be working and the green rectangle is taking as much space as the grey rectangle.
Can anyone suggest improvements to my xml-defined shape?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="15dp">
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
<item android:left="5dp" >
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<stroke
android:width="3dp"
android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
</layer-list>
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="15dp">
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
<item android:left="5dp">
<shape android:shape="rectangle" >
<solid android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
<item android:left="5dp" android:top="3dp" android:bottom="3dp" android:right="3dp">
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
</layer-list>

How to set the Top Stroke of Button?

My question is really simple. That is about Button attrs.
I want to make Button which has only Top stoke. What can I set in layout xml ??
My xml file is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:width="1dp" android:color="#color/btn_disable"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/white_pressed"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item>
<shape>
<solid android:color="#null"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp" />
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
</selector>
I cannot determine the exact shape you are looking for, but judging that you only want the top stroke, hide all other strokes, you can use a layer-list to achieve this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="your_stroke_colour" />
<corners
android:radius="your_radius" />
</shape>
</item>
<item
android:top="1dp"
android:bottom="0dp"
android:left="0dp"
android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="your_color" />
<corners
android:radius="your_radius" />
</shape>
</item>
</layer-list>

Categories

Resources