How to draw triangle shape in android xml [duplicate] - android

This question already has answers here:
android triangle drawablw xml
(3 answers)
Closed 5 years ago.
How to create shape in android using xml? and can we arrange those triangles as circle?

Try this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/rightArrow">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
For the desired direction of triangle you can play with the degrees and pivotx/y points
e.g
<rotate
android:fromDegrees="220"
android:toDegrees="0"
android:pivotX="35%"
android:pivotY="5%" >
<shape
android:shape="rectangle" >
<stroke android:color="#000000" android:width="1dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
will give you 'v' shaped triangle
alternate for rotate
this property also given v shape triangle
android:rotation="180"

Related

How to achieve rounded corner with a diagonal inside it in a shape drawable?

how to achieve rounded corner and a diagonal shape as shown in figure in android shape xml? Thanks
You can use rotate element with layer-list.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:dashGap="5dp"
android:dashWidth="8dp"
android:width="8dp"
android:color="#989898"/>
<corners android:radius="10dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="60"
android:toDegrees="45"
android:pivotX="50%"
android:pivotY="50%">
<shape
android:shape="line">
<stroke
android:dashGap="5dp"
android:dashWidth="8dp"
android:width="8dp"
android:color="#989898"/>
</shape>
</rotate>
</item>
</layer-list>

trouble with layer-list in xml drawable?

I am trying to design a layout using xml. it's a chat bubble layout. I just need a shape to be place after a shape. basically i am using 2 rectangles 1 rectangle rotated by 45 degree. i need tilted rectangle to placed after a normal rectangle.
<?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%"
android:toDegrees="0" >
<shape android:shape="rectangle" >
<solid android:color="#color/chat_incoming_color" />
</shape>
</rotate>
</item>
<item android:left="16dp">
<shape android:shape="rectangle" >
<solid android:color="#color/chat_incoming_color" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
http://imgur.com/j556772
image is shown in the link in that image first part is ok but second layout's background should be made accordingly..
Try this I hope it will help you to understand your problem.
<?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="-35%"
android:pivotY="91%"
android:toDegrees="0">
<shape>
<solid android:color="#android:color/darker_gray"/>
</shape>
</rotate>
</item>
<item android:left="16dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/darker_gray"/>
<corners android:radius="4dp"/>
</shape>
</item>
</layer-list>

Create a rectangle with triangle drawable android?

I need to create a drawable shape XML as per the below image in which we have a triangle in the middle of the upper side.
I do know how to create a rectangle and a triangle
Rectangle drawable
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>
Triangle drawable
<?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="#android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
How to use both of them to create that image?
It seems a 9 patch like this small thingy here
can really be what you are looking for:
Just give it the .9.png extension.
For a nice guide on 9 patches, see: http://radleymarx.com/blog/simple-guide-to-9-patch/

Triangle Shape - How does it work?

I found several answers to How to draw a triangle shape in Android in the resources files. But I don't found any to explain how I can change the triangle rotation.
Example that I found:
<?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="#color/color_app_transparent"
android:width="10dp" />
<solid
android:color="#color/color_app_primary" />
</shape>
</rotate>
</item>
</layer-list>
Somebody can explain how can I change the Triangle Shape to do a arrow like this:
I manage to draw that arrow, here is the code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/triangle_shape">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="0%"
android:pivotY="140%" >
<shape
android:shape="rectangle" >
<stroke
android:color="#color/color_app_transparent"
android:width="2dp" />
<size android:width="50dp" android:height="50dp"/>
<solid
android:color="#color/color_app_primary" />
</shape>
</rotate>
</item>
</layer-list>
For what I understand, I had to rotate the square 45º and then I tried to adjust the X and Y points to manage to hide most of the square and show only one edge and this way I made the arrow.
The shape after rotating 45º:
And then the final result after changing the points X and Y:
I used a ImageView to display my arrow with width and height as wrap_content.

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