Draw a marker on a gridview item - android

I would like to draw a triangle on the upper right corner of a gridview item as a marker. I can do this with a background image but would like to use shape drawing with xml because this way things look more georgeous :D

Use below 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="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#color/transparent" android:width="10dp"/>
<solid
android:color="#color/your_color_here" />
</shape>
</rotate>
</item>
</layer-list>

There are already numerous resources available on the internet , However, the first link mentioned was helpful to me. However u can check below ones!
This link might help u out in achieving the goal.
Using Android Specification to draw triangle.
or drawing Triangle Using Canvas
Drawing Triangle Using Canvas

Related

How to achieve a rounded Triangle shape on a Rectangle view in android

I am trying to create a Triangle on top of a Rectangular View in Android. I am using this drawable xml to create a Triangle View. But I am not able to get the rounded edge. How to achieve this. Can someone help ?
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="-135"
android:pivotX="90%"
android:pivotY="-45%">
<shape
android:shape="rectangle">
<solid
android:color="#android:color/black"/>
</shape>
</rotate>
</item>
What I am trying to achieve :
What I am getting in Device :

Creating triangle shape xml without wasting space android

The solutions I have found over internet cuts the rectangle but I am having like half of it without nothing and wasting space.
For example:
<?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="#color/black" />
</shape>
</rotate>
</item>
</layer-list>
This code creates a triangle, but I have a lot of space wasted, look at the image:
I want to create a triangle that "fills" the image view. Something like this:
Not necessarily has to be equilateral. Any ideas?
Well, if it is okay for you to create the shape in java , you can do it by extending the Drawable class in Android. See this

Triangle shape using xml in Android

I a created triangle shape following this post
<?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/transparent" android:width="10dp"/>
<solid
android:color="#color/your_color_here" />
</shape>
</rotate>
</item>
But I have not been able to incline it towards the right or the left.
Any help is appreciated.
Thanks
I think you could create this layout by overlaying different shapes one after another like it is shown in the selected for best answer in this question Android: Drawing custom shapes First <item> could be your rectangle and then you could use one rectangle and one triangle for the pointer

Create specific square shape android

I've created a button, when it's clicked, it's like the picture below.
For now, it's an image created with Gimp and 9-patch. It works, but I want to know if there is a way to create the same image with a Shape in XML (only the blue part in the middle). So in the future, I can change for example the color very easily.
Thanks in advance.
Edit :
I tried something :
<item >
<shape android:shape="rectangle">
<solid android:color="#color/blue1"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<solid
android:color="#android:color/transparent" />
</shape>
</rotate>
</item>
But, of course, it doesn't work. The transparent triangle just show the blue rectangle in the background. Maybe with two triangle and one square, but i'm really weak in android shape system.

Shape Drawable with only two sides

I am trying to implement in XML a ShapeDrawable like this, but so far without success.
How do I make the Stroke visible only for two sides?
Is that even possible?
Otherwise what cloud I use (I seed it as background of a TextView).
Here is a solution which is using a LayerListDrawable:
background_white_lateral_border.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/white" />
</shape>
</item>
<item>
<inset
android:insetLeft="1dp"
android:insetRight="1dp" >
<shape android:shape="rectangle" >
<solid android:color="#android:color/black" />
</shape>
</inset>
</item>
</layer-list>
Note: This firstly draws a white rectangle and then a black rectangle with a left and right inset of 1dp on top of it, giving the effect of lateral borders. It's just something to keep in mind, in case you worry about performance (which is in my opinion negligible for minor styling like this).
AFAIK, that is not possible with a single ShapeDrawable. A nine-patch PNG, or a LayerListDrawable of two ShapeDrawables (one per line) should work though.

Categories

Resources