This is the desired drawable which I am trying to build.
The problem:
As I know layer-list size depends on the last item in it. But if I use android:drawable inside tag <item>, layer list gets cropped to the drawable size doesn't matter in what position it is.
Here is my swipe_close.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="34dp"
android:useLevel="false">
<solid android:color="#ffffff" />
</shape>
</item>
<item android:drawable="#drawable/ic_close_swipe" />
<item>
<shape
android:innerRadius="34dp"
android:shape="ring"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#E6E6E6" />
</shape>
</item>
</layer-list>
And the ic_close_swipe.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="39dp"
android:height="39dp"
android:viewportWidth="39"
android:viewportHeight="39">
<path
android:pathData="M37.7269,5.2568C38.5079,4.4757 38.5079,3.2094 37.7269,2.4283L36.5711,1.2726C35.7901,0.4916 34.5238,0.4916 33.7427,1.2726L20.7758,14.2395C19.9947,15.0206 18.7284,15.0206 17.9473,14.2395L4.9804,1.2726C4.1994,0.4916 2.933,0.4916 2.152,1.2726L0.9962,2.4283C0.2152,3.2094 0.2152,4.4757 0.9962,5.2568L13.9632,18.2237C14.7442,19.0048 14.7442,20.2711 13.9632,21.0521L0.9962,34.0191C0.2152,34.8001 0.2152,36.0665 0.9962,36.8475L2.152,38.0032C2.933,38.7843 4.1994,38.7843 4.9804,38.0032L17.9473,25.0363C18.7284,24.2553 19.9947,24.2553 20.7758,25.0363L33.7427,38.0032C34.5238,38.7843 35.7901,38.7843 36.5711,38.0032L37.7269,36.8475C38.5079,36.0665 38.5079,34.8001 37.7269,34.0191L24.7599,21.0521C23.9789,20.2711 23.9789,19.0048 24.7599,18.2237L37.7269,5.2568Z"
android:fillColor="#C4C4C4"/>
</vector>
I would recommend changing swipe_close.xml as follows.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="34dp"
android:useLevel="false">
<solid android:color="#ffffff" />
</shape>
</item>
<item
android:bottom="25dp"
android:drawable="#drawable/ic_close_swipe"
android:left="25dp"
android:right="25dp"
android:top="25dp" />
<item>
<shape
android:shape="ring"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#E6E6E6" />
</shape>
</item>
</layer-list>
Hope that helps!
Related
As shown in the image below i cannot clip the corners of a layer corresponding to another layer. Is there any way that it can be done that I am missing here? I want to clip anything that's going out of the corner radius.
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/darkColor">
<shape android:shape="rectangle">
<solid android:color="#color/color_e95252" />
<corners android:radius="16dp"/>
</shape>
</item>
<item android:id="#+id/lightColor1"
android:bottom="40dp"
android:right="300dp">
<shape android:shape="rectangle">
<solid android:color="#color/color_ea5f5f" />
<corners android:radius="16dp"/>
</shape>
</item>
**The item below is kinda unnecessary**
<item android:id="#+id/lightColor2"
android:right="90dp"
android:top="-40dp"
android:bottom="-10dp"
android:left="10dp"
android:width="300dp"
>
**This is where the problem occurs**
<rotate android:fromDegrees="8">
<shape android:shape="rectangle">
<solid android:color="#color/color_ea5f5f" />
<corners
android:bottomRightRadius="300dp"
android:topLeftRadius="#dimen/dim_16"
android:bottomLeftRadius="50dp"/>
</shape>
</rotate>
</item>
Here you go, don't need that extra xml.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/darkColor">
<shape android:shape="rectangle">
<solid android:color="#color/appThemeColor" />
<corners android:radius="16dp"/>
</shape>
</item>
<item android:id="#+id/lightColor1"
android:bottom="30dp"
android:right="300dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
</shape>
</item>
<item android:id="#+id/lightColor2"
android:top="-50dp"
android:width="300dp"
>
<rotate android:fromDegrees="8">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners
android:bottomRightRadius="250dp"
android:topLeftRadius="60dp"
android:bottomLeftRadius="50dp"/>
</shape>
</rotate>
</item>
</layer-list>
Hi guys so i created 2 xmls for 3 shapes. 2 shapes are rectangle 1 is ring.
First XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorGrey"/>
<size android:height="40dp" android:width="10dp"/>
</shape>
Second XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorWhite"/>
<size android:height="40dp" android:width="40dp"/>
</shape>
Then i created a layer-list drawable like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/rectangle"
android:gravity="center" />
</item>
<item android:top="40dp">
<bitmap android:src="#drawable/circle"
android:gravity="center" />
</item>
<item android:top="40dp">
<bitmap android:src="#drawable/rectangle"
android:gravity="center" />
</item>
</layer-list>
My main goal was to create this. Main problem is they are stacking top of each other.
Try this (I've assumed a 40x40dp image size based on your code and a white background based on your image):
<?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/white"/>
<size android:height="40dp" android:width="40dp"/>
</shape>
</item>
<item
android:right="18dp"
android:left="18dp">
<shape
android:shape="rectangle">
<solid android:color="#color/green"/>
<size android:height="40dp" android:width="4dp"/>
</shape>
</item>
<item android:gravity="center">
<shape
android:shape="oval">
<solid android:color="#color/green"/>
<size android:height="15dp" android:width="15dp"/>
<stroke android:width="2dp" android:color="#color/white"/>
</shape>
</item>
</layer-list>
I made a shape for a custom button and it fills the whole background of the button instead of showing the shape.
This is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="ring"
android:useLevel="false"
android:thicknessRatio="0">
<solid android:color="#color/white" />
<size
android:width="1dp"
android:height="1dp" />
</shape>
</item>
<item android:left="0.75dp" android:top="1dp" android:bottom="0.5dp" android:right="1.5dp">
<rotate android:fromDegrees="" android:pivotX="0%" android:pivotY="0%" android:toDegrees="0">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<size android:height="1dp" android:width="1dp"/>
</shape>
</rotate>
</item>
</layer-list>
I found the solution. For some reason when I use the code I posted in an Imageview it shows correctly, but if I try to use it for a background (doesn't matter if it is for an Imageview/button/etc it shows as a square.
The problem seemed to be that the Preview doesn't represent at all the shape as a background, so I had to check the layout all the time and do the changes depending on it, and not the preview.
This is the final code:
<?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="#color/white" />
<size
android:width="5dp"
android:height="5dp" />
</shape>
</item>
<item android:left="8dp" android:top="30dp" android:bottom="8dp" android:right="30dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<size android:height="1dp" android:width="1dp"/>
</shape>
</item>
</layer-list>
I want to make custom radio button.I have created a drawable for normal(radio_normal) state.But I find it difficult to create a shape for checked state(radio_pressed).I have tried creating my desired way.But didn't work out well.I don't know if I have done it the right way.For checked state the shape should look like this.The outer border and the inner circle must be white in color.Can anybody help me with that?
radio_normal:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid
android:color="#2196f3"></solid>
</shape>
radio_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#android:color/transparent">
</solid>
<size
android:height="60dp"
android:width="60dp">
</size>
<stroke
android:color="#ffffff"
android:width="1dp"
>
</stroke>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid
android:color="#ffffff">
</solid>
</shape>
<size
android:height="30dp"
android:width="30dp">
</size>
</item>
</layer-list>
radio_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/radio_pressed"
>
</item>
<item
android:state_checked="false"
android:drawable="#drawable/radio_normal"
>
</item>
</selector>
activity_main:
<RadioButton
android:id="#+id/radio"
android:layout_centerInParent="true"
android:layout_width="10dp"
android:button="#drawable/radio_bg"
android:layout_marginTop="100dp"
android:layout_height="10dp"
/>
This is about as close as I can get you, hope it works on your side and that it helps :)
radio_normal:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#2196f3" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
radio_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:padding="3dp">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
radio_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/radio_pressed"
>
</item>
<item
android:state_checked="false"
android:drawable="#drawable/radio_normal"
>
</item>
</selector>
activity_main:
<RadioButton
android:id="#+id/radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:button="#drawable/radio_bg"/>
Let me know if this worked for you :)
Is this maybe what you were looking for ?
radio_pressed:
<?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="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
Reaction to comment:
The code:
<?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="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="150dp"
android:height="150dp" />
</shape>
</item>
<item
android:bottom="50dp"
android:left="50dp"
android:right="50dp"
android:top="50dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
Note: The only changes made was in the sizes to accomodate your imageview of 150dp X 150dp. When using it for your radio button I highly reccomend using the 10x10dp sizes
i want to create a button like this :
as you see the button is curved from the middle to the inside.
i have tries something like this but it's far from what i want.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false">
<size
android:width="50dp"
android:height="50dp" />
<solid android:color="#android:color/darker_gray" />
</shape>
</item>
</layer-list>
how can i do this ? is it possible to create it with xml drawables ?
This will help you... try...
<?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="#FF0000" />
<size android:height="100dp"
android:width="200dp"/>
</shape>
</item>
<item
android:right="160dp"
android:top="7dp"
android:bottom="7dp"
android:left="-90dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="2dp"
android:shape="oval"
android:useLevel="false">
<size
android:width="50dp"
android:height="50dp" />
<solid android:color="#android:color/darker_gray" />
</shape>
</item>
</layer-list>