Triangle Shape - How does it work? - android

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.

Related

How to draw triangle shape in android xml [duplicate]

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"

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>

Android set triangle drawable to Button drawable left

i need some explanation how to set triangle.xml to Button drawable left.
<?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="13%"
android:pivotY="-40%"
android:toDegrees="45" >
<shape
android:shape="rectangle" >
<stroke
android:width="10dp"
android:color="#F000" />
<solid android:color="#color/Lime" />
</shape>
</rotate>
</item>
</layer-list>
So i have drawable of triangle and it wont show up when i put it as DrawableLeft of button.
is there are other way to make triangle as pure shape or some method to make it work?
Your Button XML should look like below
<Button
android:id="#+id/YourButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Name of the button"
android:drawableLeft="#drawable/drawablefilename"
android:onClick="listener"
>
</Button>
In your Activity.java file, include this line
btnName.setCompoundDrawablesWithIntrinsicBounds( R.drawable.drawablefilename, 0, 0, 0);
It might wor
My bad found my problem. It needs to set size.
<?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="13%"
android:pivotY="-40%"
android:toDegrees="45" >
<shape
android:shape="rectangle" >
****<size android:width="15dp" android:height="15dp"/>****
<stroke
android:width="10dp"
android:color="#F000" />
<solid android:color="#color/Lime" />
</shape>
</rotate>
</item>
</layer-list>

Android - rotate cuts off corners of shape

I wish to make a diamond as a resource file so I am rotating a square in order to do so. The problem is that the corners seem to be getting cut off, making a hexagon instead. Also as a side note, I'd like to stretch it vertically if that's possible.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="45"
>
<shape android:shape="rectangle">
<size
android:height="40dp"
android:width="40dp" />
<solid
android:color="#color/level_1_color" />
</shape>
</rotate>
</item>
Heres how:
**<item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">**
<rotate
android:fromDegrees="45"
android:toDegrees="45" >
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/sym_orange_bdfm"/>
<size
android:width="23dp"
android:height="23dp"/>
</shape>
</rotate>
</item>
Adjust Left/Right/Top/Bottom as needed.
This was already solved here: Diamond shape xml background for android view
Just change fill color and stroke width according to your needs.
I am searching for rotate the square this help
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp">
<rotate
android:drawable="#color/colorAccent"
android:fromDegrees="-135"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="-45"
android:visible="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimaryDark" />
<corners android:radius="15dp" />
<size
android:width="90dp"
android:height="90dp" />
<solid
android:angle="45"
android:endColor="#color/colorAccent"
android:gradientRadius="34"
android:startColor="#color/colorPrimary" />
</shape>
</rotate>
</item>
</layer-list>
But I follow some ratio relationship for height and width 90dp and item top ,bottom,left ,right are 20dpenter image description here

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