How can I create a vertical line in a drawable xml? Android - android

Is there a way to make a drawable that draws a vertical line? So far I have a horizontal line that draws this:
current code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/object_switcher_background" />
<item android:left="0dp"
android:right="250dp">
<shape android:shape="line">
<stroke android:color="#color/c19"
android:width="2dp"/>
<size android:width="40dp"/>
</shape>
</item>
<item android:left="50dp"
android:bottom="50dp"
android:top="75dp"
android:right="150dp">
<shape android:shape="line">
<stroke android:color="#color/c19"
android:width="2dp"
android:rotation="90"/>
<size android:width="40dp"/>
</shape>
</item>
</layer-list>
How can I manipulate this to make it look like this?

You could do this by nesting your second item inside a rotate item.

you can do that using rotate item: the below code defines a white background with a vertical divider using only xml drawable
<?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/white" />
</shape>
</item>
<item>
<rotate android:fromDegrees="90">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#android:color/darker_gray" />
</shape>
</rotate>
</item>

I'm not sure why you need a drawable for this.
This can also be done by using a simple view and adjusting its height, width and position according to your needs. For example:
<View
android:layout_width="1dp"
android:background="#color/grey_text_color"
android:layout_height="match_parent"/>

Related

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>

How to set shadow to line?

I do not know how to do it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line">
<stroke android:width="5dp"
android:color="#698cc8"
/>
</shape>
</item>
</selector>
Something like this:
https://i.stack.imgur.com/npZm0.png
I think it will help you,
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line">
<stroke android:width="8dp"
android:color="#c7c7c7"
/>
</shape>
</item>
<item android:bottom="4dp">
<shape android:shape="line">
<stroke android:width="5dp"
android:color="#698cc8"
/>
</shape>
</item>
</layer-list>
out put is,
give needed shade color in first item. then give bottom value in second item.
which means second item height get reduce from bottom corresponding to bottom value.there for we can visible shade color below the line.

Shape with multiple circles

I want to draw a circle of color 1 inside a circle of color 2. Both of them have stroke and its inside is transparent. This should be used as a background of a button.
Something like this (but green and red are next to each other - no gap in between. Sorry, I don't have any graphical program where I could draw them so they're next to each other.)
Is there a way how to do it in XML ?
I was thinking of doing it using shape XML but layers are scaled so layers would be on top of each other so only 1 color would be visible.
You use a layer list with insets in order to avoid the inner circle to lurk out from the outer circle, like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke
android:width="4dp"
android:color="#android:color/holo_red_dark" />
</shape>
</item>
<item>
<inset android:inset="4dp">
<shape android:shape="oval">
<stroke
android:width="4dp"
android:color="#android:color/holo_green_dark" />
</shape>
</inset>
</item>
</layer-list>
You set the size and background of the button like this:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/red_green" />
It then looks like this:
Try something like 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:height="10dp" android:width="10dp"/>
<stroke
android:width="2dp"
android:color="#FF0000"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<stroke
android:width="1dp"
android:color="#00FF00"
/>
</shape>
</item>
</layer-list>
You can change the width and height of the size element to get a different ring width.
You can achieve this by defining two drawable resources both circle with different stroke colors and puting them in a layer-list drawable one on top of the other, with different ofsets which give then the effect you need:
Inner circle inner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorRed"
android:width="2dp"/>
</shape>
Outer circle outer.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorGreen"
android:width="2dp"/>
</shape>
Button background background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/outer"
android:right="0dp"
android:top="0dp"
android:bottom="0dp"
android:left="0dp"
android:gravity="center"/>
<item android:drawable="#drawable/inner"
android:right="4dp"
android:top="4dp"
android:bottom="4dp"
android:left="4dp"
android:gravity="center"/>
</layer-list>
And this is final result

Making android custom drawable

How can one create such a drawable that will be matching height of given layout ? I know I have to create a some sort of multiple shapes within one drawable but I am kinda lost in this.
Try layer-list of 2 oval shapes and a line. Then set as the background of your view. I'm not sure if it works.
You can also use 9 patch: read this
It should work with xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="4dp">
<item>
<rotate android:fromDegrees="90" android:gravity="center_horizontal" android:toDegrees="90">
<shape android:shape="line">
<stroke android:color="#000000" android:width="1dp"/>
</shape>
</rotate>
</item>
<item android:gravity="center_horizontal|top" android:height="8dp" android:width="8dp">
<shape android:shape="oval">
<solid android:color="#000000"/>
</shape>
</item>
<item android:gravity="center_horizontal|bottom" android:height="4dp" android:width="4dp">
<shape android:shape="oval">
<solid android:color="#000000"/>
</shape>
</item>
</layer-list>

How to define a rectangle with a circle at the end using Android XML definitions

Is it possible to define a shape like shown on the following image:
I tried a layer-list but I could not find a solution close to what I am looking for.
I want to use the resulting shape as a background image to a RelativeLayout with a transparency.
Any hints are appreciated! Thank you.
Here is the shape you asked for:
<?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="250dp"
android:height="100dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item
android:top="0dp">
<shape android:shape="line">
<stroke
android:width="25dp"
android:color="#000000" />
</shape>
</item>
<item
android:right="0dp"
android:left="150dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Screenshot:

Categories

Resources