Android how to move XML drawable shape relative to other shapes - android

I want to create a clock type thing. This code does what I want, but I feel like I am doing it wrong:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="100dp"
android:height="100dp"/>
<item>
<shape android:shape="oval">
<size
android:width="100dp"
android:height="100dp"/>
<stroke
android:width="1.5dp"
android:color="#FFF" />
</shape>
</item>
<item android:top="25dp" android:left="25dp" android:right="25dp" android:bottom="25dp">
<shape android:shape="oval">
<stroke
android:width="1.5dp"
android:color="#FFF" />
</shape>
</item>
<item android:top="0dp" android:right="49.25dp" android:left="49.25dp" android:bottom="75dp">
<shape android:shape="rectangle">
<solid
android:color="#0FFF00" />
</shape>
</item>
</layer-list>
The green tick mark I feel like I should be able to define using a <size> tag and be able to position more elegantly than defining the empty space using margins around the item.

Related

Android drawable xml reverse corners

I'm trying to replicate the following shape in Android studio:
I can't figure out how to do it in android drawable xml.
At the moment I used a layer-list where:
As the first item I placed a transparent (or white) rectangle, to which I gave a padding on the left side and vertically.
As the second item I placed a rectangle containing the gradient and the corners on the left.
Now, I don't know how I can make the two "reverse angles" on the right, I've tried everything but online there aren't many examples to refer.
Here's my current 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/transparent" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="0dp"
android:top="8dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#color/green"
android:startColor="#color/blue"
android:type="linear" />
<corners
android:bottomLeftRadius="22dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="22dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
How can I change the code to obtain this shape?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="600dp"
android:height="400dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:bottomRightRadius="100dp" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp"
android:top="300dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:topRightRadius="100dp" />
</shape>
</item>
<item android:width="200dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
<item android:width="600dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
<corners android:radius="100dp" />
</shape>
</item>
</layer-list>
Here is a preview image
try this

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>

Android xml shape fills the whole background

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>

Android Custom shape for EditText

I am trying to create a custom design for EditText. See the attached image below,
I am trying to achieve this using a custom shape like the one defined below.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/places_ic_search"
android:width="30dp">
<shape
android:shape="rectangle"></shape>
</item>
<item
android:left="30dp"
android:drawable="#color/white">
<shape android:shape="rectangle" >
<gradient android:startColor="#000000" android:endColor="#FFFFFF" android:angle="90"/>
</shape>
</item>
</layer-list>
But the search icon is not displayed. How can I achieve this?
Thanks.
I have applied the answer by tiny sunlight and the EditBox looks like this, Search icon is no aligned to the left.
I edit my answer. Edit Shape.xml as below:
Shape.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<stroke android:width="5dp" android:color="#FF0000" />
</shape>
</item>
<item
android:left="3dp"
android:right="3dp"
android:top="3dp"
android:bottom="3dp"
>
<shape android:shape="rectangle" >
<stroke android:width="1dp" android:color="#FF0000" />\
<solid android:color="#FFFFFF" />
<corners android:radius="3dp" />
</shape>
</item>
<item android:gravity="left" android:left="10dp">
<bitmap android:src="#android:drawable/ic_menu_search" android:gravity="left">
</bitmap>
</item>
</layer-list>
I would do it like this :
For the border part, you could do it with a drawable like you did
For the search icon, instead of trying to put it in the drawable file, why not set it in you EditText xml layout, using android:drawableLeft, you can then set padding using android:paddingLeft and android:drawablePadding
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF" android:angle="90"/>
<corners android:radius="3dp"></corners>
</shape>
</item>
<item android:drawable="#drawable/places_ic_search"
android:gravity="center_vertical|left"
android:height="10dp"
android:width="30dp">
</item>
</layer-list>

XML shape android

I have an oval shape that I made rounded, and i want to add a smaller oval in the center of this one with another color. When I try to do this, I can see the smallest and they always have the same size
Can i overlay two shapes without masking the other? i did this but I can see the smallest shape :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
>
<shape android:shape="oval">
<size android:width="170dp"
android:height="170dp"/>
<solid android:color="#f00"/>
</shape>
</item>
<item >
<shape android:shape="oval"
>
<size android:width="140dp"
android:height="140dp"/>
<solid android:color="#f000"/>
</shape>
</item>
</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="oval">
<size android:width="170dp"
android:height="170dp"/>
<solid android:color="#f00"/>
</shape>
</item>
<item
android:left="15dp"
android:right="15dp"
android:top="15dp"
android:bottom="15dp">
<shape android:shape="oval" >
<size android:width="140dp"
android:height="140dp"/>
<solid android:color="#f000"/>
</shape>
</item>
</layer-list>
It will display as this image
Add
android:left="6dp"
android:right="6dp"
android:top="6dp"
android:bottom="6dp"
To the inner item tag and check

Categories

Resources