android triangle drawablw xml - android

I want to draw an equilateral triangle.I have checked but it is inverted.I want a triangle that looks like the image below.
Triangle:
triangle_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#fff" android:width="1dp"/>
<solid
android:color="#000" />
</shape>
</rotate>
</item>
</layer-list>
Currently it looks like this..

Usign Vector drawable you can achieved your shape like below:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportHeight="100"
android:viewportWidth="100">
<group android:name="triableGroup">
<path
android:name="triangle"
android:fillColor="#color/color_start_color_back"
android:pathData="m 50,0 l 50,50 -100,0 z" />
</group>
</vector>
Output:
I hope its helps you.

Try this way it will work
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45" >
<shape android:shape="rectangle" >
<stroke
android:width="10dp"
android:color="#00000000" />
<solid android:color="#00ACED" />
</shape>
</rotate>
</item>
</layer-list>
OUTPUT

I have found out the way to make it look like an up facing triangle.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="-45"
android:toDegrees="45"
android:pivotX="270%"
android:pivotY="70%" >
<shape
android:shape="rectangle" >
<stroke android:color="#000000" android:width="1dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>

Related

Drawing a tooltip with a border

I am trying to achieve an a bordered tooltip like this (well not exactly, just the part where I want to have a smooth border):
So far, this is what I have tried:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:gravity="top|right">
<rotate android:fromDegrees="45" android:toDegrees="0"
android:pivotX="50%" android:pivotY="0%" >
<shape android:shape="rectangle">
<size android:width="24dp" android:height="24dp" />
<stroke android:color="#android:color/holo_blue_bright" android:width="1dp"/>
</shape>
</rotate>
</item>
<item>
<shape android:shape="rectangle">
<size android:width="206dp" android:height="76dp" />
<solid android:color="#color/white"/>
<stroke android:color="#android:color/holo_blue_bright" android:width="1dp"/>
<corners android:radius="2dp" />
</shape>
</item>
<item android:gravity="top|right">
<rotate android:fromDegrees="45" android:toDegrees="0"
android:pivotX="50%" android:pivotY="0%" >
<shape android:shape="rectangle">
<size android:width="24dp" android:height="24dp" />
<solid android:color="#color/white"/>
</shape>
</rotate>
</item>
</layer-list>
Here's my output:
I got something working, though it's not perfect. Here's the output:
Notice the edges are not very smooth. Here's the modified code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="top|right" android:right="10dp">
<rotate android:fromDegrees="45" android:toDegrees="0"
android:pivotX="50%" android:pivotY="0%" >
<shape android:shape="rectangle">
<size android:width="24dp" android:height="24dp" />
<stroke android:color="#android:color/holo_blue_bright" android:width="1dp"/>
</shape>
</rotate>
</item>
<item>
<shape android:shape="rectangle">
<size android:width="206dp" android:height="76dp" />
<solid android:color="#color/white"/>
<stroke android:color="#android:color/holo_blue_bright" android:width="1dp"/>
<corners android:radius="2dp" />
</shape>
</item>
<item android:gravity="top|right" android:right="10dp" android:top="1dp">
<rotate android:fromDegrees="45" android:toDegrees="0"
android:pivotX="50%" android:pivotY="0%" >
<shape android:shape="rectangle">
<size android:width="24dp" android:height="24dp" />
<solid android:color="#color/white"/>
</shape>
</rotate>
</item>
</layer-list>
You should just use 9-patch. On Android Studio, choose "create 9-patch file" :
Then, have the left and top marking for the parts you wish to duplicate the larger/smaller you have the image.
And have the right and bottom markings for the parts you wish to have the content.
On many cases, those are the same areas. but on your case, I think it won't be as such.
I also suggest to make the image as small as possible, so that it will take minimal size that you allow it to be. This would save a bit of space.
You can read more about those on the docs:
https://developer.android.com/guide/topics/graphics/drawables#nine-patch
https://developer.android.com/studio/write/draw9patch

Drawing 2 rectangle with cut inbetween

i want to draw in xml with given exmaple like this.
i tried with this solution.
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:right="100dp">
<shape android:shape="rectangle">
<size android:height="100dp" android:width="100dp"/>
<solid android:color="#android:color/black"/>
</shape></item><item android:left="100dp"><shape android:shape="rectangle">
<size android:height="100dp" android:width="100dp"/>
<solid android:color="#android:color/holo_green_light"/>
</shape></item></layer-list>
but it doesnt have middle cut between 2 shapes.also given shape should work with any width .so dont want do define any width restrictions
result1
Try this...
use this style
<item >
<shape android:shape="rectangle">
<solid android:color="#FFCD80"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<rotate android:fromDegrees="45" android:toDegrees="0" android:pivotX="20%" android:pivotY="10%" >
<shape android:shape="rectangle" >
<solid android:color="#FF0000" />
<corners android:radius="5dp"/>
</shape>
</rotate>
</item>
layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
tools:context=".game.GameActionActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_style_twocolor_center_crossline" />
</RelativeLayout>
change color as you what.
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#fff8bb"
android:pathData="M0,0
L0,24
L24,24
L24,0 z" />
<path
android:fillColor="#ffc5bb"
android:pathData="M0,14
L8,24
L22,24
L8,0
L0,0z" />
</vector>
In xml file
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#drawable/cust_rect"/>

Diamond shape xml background for android view

Hi anyone please suggest me how to create android xml drawable like below image?
Now I am using below code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45">
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#color/colorAccent" />
<solid android:color="#color/colorAccent" />
</shape>
</rotate>
</item>
</layer-list>
but its result is triangle but I need above shape.
Check this,
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="43dp"
android:top="43dp"
android:right="43dp"
android:bottom="43dp">
<rotate android:fromDegrees="45">
<shape>
<size
android:width="200dp"
android:height="200dp" />
<gradient
android:startColor="#0fdcc9"
android:endColor="#0fdcc9" />
<stroke
android:width="5dp"
android:color="#2DACA0" />
</shape>
</rotate>
</item>
</layer-list>
This is my drawable file for diamond shape ic_diamond.xml file.
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="372.7dp"
android:height="367.4dp"
android:viewportWidth="372.7"
android:viewportHeight="367.4">
<path
android:fillColor="#50AE89"
android:strokeColor="#266D4F"
android:strokeWidth="5"
android:strokeMiterLimit="10"
android:pathData="M188.706,51.8702 L318.105,181.269 L183.968,315.406 L54.5691,186.007
L188.706,51.8702 Z" />
</vector>
I include this in simple view:
<View
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:background="#drawable/ic_diamond"
/>
Here, is the result:
i fixed with this tutorial
here
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="43dp"
android:left="43dp"
android:right="43dp"
android:state_pressed="true"
android:top="43dp">
<rotate android:fromDegrees="45">
<shape>
<size
android:width="200dp"
android:height="200dp" />
<gradient
android:endColor="#color/colorPrimaryDark"
android:startColor="#color/colorPrimaryDark" />
<stroke
android:width="5dp"
android:color="#color/colorPrimary" />
<corners android:radius="7dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</rotate>
</item>
</layer-list>
Here is a drawable resource which creates a 200dp square and rotates it 45° to form a diamond shape:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="50dp"
android:left="50dp"
android:right="50dp"
android:top="50dp">
<rotate android:fromDegrees="45">
<shape>
<size
android:width="200dp"
android:height="200dp"/>
<stroke
android:width="10dp"
android:color="#358165"/>
<solid android:color="#69BE9E"/>
</shape>
</rotate>
</item>
</layer-list>

Speech bubbles in android

Hi I wanted to use speech bubbles as background images for my app. I came across this posted here
Android drawable speech bubble
The example starts from the right direction I wanted to achieve this in the opposite direction I have tried
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="30dp">
<rotate
android:fromDegrees="-45"
android:pivotX="0%"
android:pivotY="0%"
android:toDegrees="0" >
<shape android:shape="rectangle" >
<solid android:color="#CCC" />
</shape>
</rotate>
</item>
<item android:right="10dp">
<shape android:shape="rectangle" >
<solid android:color="#CCC" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
But I'm unable to get it to work, any help would be greatly appreciated!
Incoming message:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="-45"
android:pivotX="0%"
android:pivotY="0%" >
<shape android:shape="rectangle">
<solid android:color="#color/chat_message_background_incoming" />
</shape>
</rotate>
</item>
<item android:left="20dp">
<shape
android:shape="rectangle">
<solid android:color="#color/chat_message_background_incoming"/>
<corners
android:radius="1dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="5dp"
android:topRightRadius="15dp" />
</shape>
</item>
</layer-list>
Outgoing message:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="100%"
android:pivotY="0%" >
<shape android:shape="rectangle">
<solid android:color="#color/chat_message_background_outcoming" />
</shape>
</rotate>
</item>
<item android:right="20dp">
<shape
android:shape="rectangle">
<solid android:color="#color/chat_message_background_outcoming"/>
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="5dp" />
</shape>
</item>
</layer-list>
Hopefully, it would help
incoming bubble shape background
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="-45"
android:pivotX="100%"
android:pivotY="100%"
android:toDegrees="0" >
<shape android:shape="rectangle" >
<solid android:color="#color/colorSendMessageBg" />
</shape>
</rotate>
</item>
<item android:right="5dp">
<shape android:shape="rectangle" >
<solid android:color="#color/colorSendMessageBg" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
outgoing bubble shape background
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="0%"
android:pivotY="100%"
android:toDegrees="0" >
<shape android:shape="rectangle" >
<solid android:color="#color/colorReciveMessageBg" />
</shape>
</rotate>
</item>
<item android:left="5dp">
<shape android:shape="rectangle" >
<solid android:color="#color/colorReciveMessageBg" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
Final result

Drawing triangle in XML [duplicate]

This question already has answers here:
Making a triangle shape using XML definitions?
(20 answers)
Closed 8 years ago.
In XML I'm attempting to draw a drop down triangle to be used as a background for a button but cant seem to get my head round the rotate XML tags.
Here's my XML code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="80%"
android:pivotY="20%">
<shape
android:shape="rectangle" >
<solid
android:color="#FFCC00" />
</shape>
</rotate>
</item>
</layer-list>
And here's the outcome of the XML:
Any ideas are appreciated.
i can draw triangle shape using XML
triangle .xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45" >
<shape android:shape="rectangle" >
<stroke
android:width="10dp"
android:color="#00000000" />
<solid android:color="#00ACED" />
</shape>
</rotate>
</item>
</layer-list>
Try out the Below
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="-45"
android:toDegrees="45"
android:pivotX="90%"
android:pivotY="-50%" >
<shape
android:shape="rectangle" >
<stroke android:color="#android:color/transparent" android:width="1dp"/>
<solid
android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>

Categories

Resources