I've created triangle shape as in code 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="13%"
android:pivotY="-40%" >
<shape
android:shape="rectangle" >
<stroke android:color="#000000" android:width="1dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
How can I make border color of the triangle different from the rest of shape? If I change stroke color it kinda works, well I have two sides with different color, without third border. How should I correct it?
Now it did take me some time to try to make a good example for you to play around with canvas. Following code can still be improved with linear/radial gradients, also you can make it more customizable if you wish. Maybe I will do it in the future, but I'm done for today.
First add this to your resource values. I use /values/attrs.xml If you do not see this file, create it.
Now create a Triangle class, preferably in a folder where you keep your custom views. Fix package name and the R class import.
And now crate a new class (this is not neeeded, but I did so that you might get the idea how to use canvas)
And now finally the layout called canvas_demo.xml
The final result will look something like this:
You can rotate it by using android:rotation attribute, for example by using 180 it would look something like this
The triangle can of course be smaller in size, it is scalable anyway. You should set it's points a,b,c if this setup doesn't suit you.
NOTE: that i'm not doing any check for arguments, so you will not get any errors if for example you set strokeWidth value that is larger than the view itself. It is your responisibilty to be careful :)
Happy Coding!