How to create 3 shape layer list top to bottom - android

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>

Related

How to design following border in android studio

Here is the image which I want to design in android studio.
This will give you the same image that you're looking for.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#color/black" />
<stroke
android:width="16dp"
android:color="#color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke
android:width="8dp"
android:color="#color/black" />
</shape>
</item>
</layer-list>
You can achieve the required output by using layer-list. In the drawable folder create a new Drawable Resource File named background.xml and replace with the following code in the file.
This is the code for background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#5271ff" />
<stroke
android:width="20dp"
android:color="#f5fffe" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#4d75f9" />
</shape>
</item>
</layer-list>

How do you create a layer-list with two solids next to each other?

I'm trying to create a drawable with two shapes next to each other using layer-list, how would I do it?
You can use the following code to have the drawable you want(Change the colors and height as you want)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<size
android:width="5dp"
android:height="5dp" />
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item android:gravity="right">
<shape android:shape="rectangle">
<size
android:width="1dp"
android:height="5dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
</layer-list>

ShapeDrawable is not drawing properly when given as background

My Current view looks like this:
I tried to draw a shape drawable with this code:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Outer green rectangle-->
<item
android:bottom="140dp">
<shape android:shape="rectangle">
<size
android:width="100dp"
android:height="200dp" />
<solid android:color="#ff5500" />
</shape>
</item>
<item
android:top="180dp"
android:bottom="140dp">
<shape android:shape="rectangle">
<size
android:width="20dp"/>
<solid android:color="#ffffff" />
</shape>
</item>
<!-- inner white rectangle -->
<item
android:bottom="-10dp"
android:right="90dp"
android:left="90dp">
<rotate
android:fromDegrees="-85">
<shape android:shape="rectangle">
<size
android:width="30dp"/>
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
and I managed to get this shape:
]2
The problem is that when I give this shape as a background of my layout, it looks like this:
Where as my final goal is to make it look like this:
change your drawable file like this
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Outer green rectangle-->
<item>
<shape android:shape="rectangle">
<solid android:color="#ff5500" />
</shape>
</item>
<item android:bottom="-720dp">
<rotate android:fromDegrees="-85">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<!-- inner white rectangle -->
</layer-list>

Android custom radiobutton

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

How can I create the following shape in Android?

I am trying to create a following shape in android.
I tried following code but it's only for changing the corners
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<gradient
android:startColor="#00EBCF"
android:endColor="#00BEEC"
android:angle="180"
android:type="linear" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
What do I need to make changes in code to achieve the above shape?
Edit1
As per the answer I used following code (modifications in layout height):
<?xml version="1.0" encoding="utf-8"?>
</shape>
</item>
<item
android:width="1500dp"
android:height="200dp"
android:top="95dp">
<shape android:shape="oval">
<solid android:color="#36D6E9" />
</shape>
</item>
But when I am using this layout as a background for LinearLayout. It's showing me this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/header_shape_rect">
use this as an example
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:height="200dp">
<shape android:shape="rectangle">
<solid android:color="#ccc"/>
</shape>
</item>
<item android:height="200dp" android:top="95dp" android:width="1500dp" >
<shape android:shape="oval">
<solid android:color="#ccc"/>
</shape>
</item>
</layer-list>

Categories

Resources