Draw Line With Starting Circle - android

I am Using Drawable For Draw Horizontal Line With Starting Small Circle In android But The Circle Comes With Center Of the Horizontal Line.
This is my code.
android->res->drawable Folder.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="start">
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="6dp"
android:useLevel="false">
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item >
<shape
android:shape="line">
<size
android:width="200dp"
android:height="200dp" />
<stroke
android:width="2dp"
android:color="#0000FF" />
</shape>
</item>
Please Help me to solve this....

I think this will solve your problem.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="line">
<size
android:width="200dp"
android:height="200dp" />
<stroke
android:width="2dp"
android:color="#0000FF" />
</shape>
</item>
<item android:gravity="start" android:left="-188dp">
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="6dp"
android:useLevel="false">
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
</layer-list>
Also, to place this drawable in imageview then follow this code.
<ImageView
android:scaleType="fitXY"
android:src="#drawable/test"
android:layout_width="200dp"
android:layout_height="15dp" />

Related

Using a layer-list with multiple circular items

I am trying to make a shape that looks roughly like
I understand that I need a layer-list with 3 items stacked on top of each other with two of them being offset from their top-left and bottom-right. Here is my code for this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="8dp" android:bottom="8dp">
<shape android:shape="oval">
<solid android:color="#color/color_white" />
</shape>
</item>
<item android:top="8dp" android:left="8dp">
<shape android:shape="oval">
<solid android:color="#color/color_gray" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/color_circle" />
</shape>
</item>
</layer-list>
But I am not getting the correct output. More specifically, the white and black shapes are smaller in size than the middle one as you can see here
I don't know why this is happening. Can anyone please help me out? Thanks
Try specifying the width and height for each shape:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="8dp" android:bottom="8dp">
<shape android:shape="oval">
<solid android:color="#android:color/white" />
<size android:width="50dp" android:height="50dp" />
</shape>
</item>
<item android:top="8dp" android:left="8dp">
<shape android:shape="oval">
<solid android:color="#android:color/black" />
<size android:width="50dp" android:height="50dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#android:color/darker_gray" />
<size android:width="50dp" android:height="50dp" />
</shape>
</item>
</layer-list>
This gives me the following image:

make one side of button curved from middle

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>

Drawable xml with horizontal line and circle in middle

I want to create a drawable xml file for the shape below. It will be like a horizontal line should fill the width of the layout and a circle in the middle.
Can anyone help me on this? Thanks in advance.
Keep it simple
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="1dp"
android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#ff0000" />
</shape>
</item>
<item
android:width="56dp"
android:height="56dp"
android:gravity="center">
<shape android:shape="oval">
<solid android:color="#ff0000" />
</shape>
</item>
</layer-list>
output
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtstep"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="6dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/simple_border_white"
android:textSize="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/round_with_shadow"
android:gravity="center"
android:text="1"
android:textColor="#color/colorPrimaryDark"
android:textSize="26sp" />
</RelativeLayout>
simple_border_white.xml
<?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/transparent" />
<!--background color of box-->
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/accentcolor" />
</shape>
</item>
</layer-list>
round_with_shadow.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/accentcolor"/>
<stroke
android:width="4dp"
android:color="#color/colorPrimaryLight"/>
<size android:width="40dp" android:height="40dp"/>
</shape>
hope this helps
Create your drawable file and put below code.
And change your color code red.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorAccent" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90">
<shape
android:shape="line"
android:top="1dp">
<stroke
android:width="0.5dp"
android:color="#color/colorAccent" />
</shape>
</rotate>
</item>
Use below drawable XML into your views background :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="100dp"
android:height="100dp"
android:gravity="center">
<shape android:shape="oval">
<solid android:color="#android:color/holo_red_dark" />
<!--background color of box-->
</shape>
</item>
<item
android:height="10dp"
android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_red_dark" />
<!--background color of box-->
</shape>
</item>
</layer-list>
Attributes height and width in <item/> are supported since 23 api.
For earlier versions you can use following code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="#ff0000" />
</shape>
</item>
<item android:gravity="center">
<shape android:shape="oval">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#ff0000" />
</shape>
</item>

How to draw vertical shape in the centre of XML drawable

How can a vertical line be drawn within an XML drawable when using a layer-list? I declared 3 shapes within the drawable but for some reason it doesn't appear as expected.
<?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/green"/>
</shape>
</item>
<item android:top="0dp" android:left="1dp" android:bottom="0dp" android:right="5dp">
<shape android:shape="rectangle">
<size android:width="3dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
<item android:top="0dp" android:left="5dp" android:bottom="0dp" android:right="1dp">
<shape android:shape="rectangle">
<size android:width="3dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
Current result
Expected result
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="16dp"
android:left="15px">
<shape
android:shape="rectangle">
<solid android:color="#color/colorAccent" />/>
<size android:width="5dip" />
<stroke android:color="#color/colorAccent" />
</shape>
</item>
<item
android:width="16dp"
android:left="80px">
<shape
android:shape="rectangle">
<solid android:color="#color/colorAccent" />/>
<size android:width="5dip" />
<stroke android:color="#color/colorAccent" />
</shape>
</item>
<item
android:width="16dp"
android:left="140px">
<shape
android:shape="rectangle">
<solid android:color="#color/colorAccent" />/>
<size android:width="5dip" />
<stroke android:color="#color/colorAccent" />
</shape>
</item>
</layer-list>
Try this solution
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />/>
<size android:width="5dip" />
<stroke
android:color="#color/colorAccent"
/>
</shape>
Include in the layout file the following
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="15dip"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#drawable/shape"></View>
<View
android:layout_width="15dip"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#drawable/shape"></View>
<View
android:layout_width="15dip"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#drawable/shape"></View>
</LinearLayout>
Change the colors and fix the margins of the layout.

Drawing a rectangle inside an oval doesn't give the desired result

I tried to make a button wherein a square shape should be inside a circle shape. It works well on a square with a circle but not with the other way around and I wonder why. Here's my xml code so far for this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="#dimen/tab_space" android:right="#dimen/tab_space">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="80dp" android:height="80dp" />
<gradient android:angle="-90" android:startColor="#990000" android:endColor="#550000" />
<stroke android:width="1dp" android:color="#110000" />
</shape>
</item>
<item android:left="#dimen/tab_space" android:right="#dimen/tab_space">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="50dp" android:height="50dp" />
<gradient android:angle="-90" android:startColor="#ff0000" android:endColor="#990000" />
<stroke android:width="1dp" android:color="#440000" />
</shape>
</item>
</layer-list>
Any solution for this?
You can try this. It worked for me.
chat_icon.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<padding android:left="7pt" android:top="8pt" android:right="7pt" android:bottom="8pt"/>
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:color="#color/jade" android:width="2dip"/>
<solid android:color="#color/white" />
</shape>
</item>
Then, to use it in your layout, declare it as follows:
<ImageView
android:id="#+id/chat_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chat_icon"
------------------------------ />

Categories

Resources