Android set triangle drawable to Button drawable left - android

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>

Related

Android: Draw shape with skewed corner

I want to use a background for my buttons. But when I use a png it slows down the app. Therefore I want to use a xml shape but I do not know how to make the corner cut (like on the picture).
By now I have the following shape which is just a rectangle:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/blue_semi_transparent"/>
<padding android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
<margin android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
</shape>
How can I draw the bottom right corner?
You can try this,
skewed_background.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="#3F8EEB" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="110"
android:pivotX="90%"
android:pivotY="90%"
>
<shape android:shape="rectangle" >
<solid android:color="#FFF" />
</shape>
</rotate>
</item>
</layer-list>
sample_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical" >
<View
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/triangle1"/>
</LinearLayout>
If you want to modify the skewed area you just need to change the value of fromDegrees, pivotX and pivotY value in the second item in skewed_background.xml.
This is exactly what you looking for
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:paddingRight="15dp"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>
bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<size
android:width="100dp"
android:height="40dp" />
<solid android:color="#13a89e" />
</shape>
</item>
<item
android:right="100dp"
android:left="100dp"
android:top="-100dp"
android:bottom="-100dp">
<rotate
android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<item
android:right="-100dp"
android:left="100dp"
android:top="-100dp"
android:bottom="-100dp">
<rotate
android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
OUTPUT
This will give you your desired result
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"/>
<item android:top="-20dp"
android:bottom="-50dp"
android:left="350dp"
android:right="-120dp">
<rotate
android:fromDegrees="10"
android:pivotX="50%"
android:pivotY="0%">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
</item>
</layer-list>

Rectangle Shape with Arrow using XML

How to implement this shape of Departments header using shape and xml without 9-patch ?
I mean rectangle with arrow on the left.
My code snippet:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1px"
android:left="#dimen/edittext_border"
android:right="#dimen/edittext_border"
android:top="#dimen/edittext_border">
<shape android:shape="rectangle">
<stroke
android:width="#dimen/edittext_border_width"
android:color="#color/menu_divider" />
<solid android:color="#color/background" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
if you are looking for someting like this, try the following code..
Make a xml arrow_shape in your drawable folder.
<?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="#c6802a" android:width="10dp"/>
<solid android:color="#c6802a" />
</shape>
</rotate>
</item>
</layer-list>
Make a xml rectangle_shape in your drawable folder.
<?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="#B2E3FA" />
</shape>
</item>
In your main xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.stpl.myapplication.MainActivity">
<RelativeLayout
android:id="#+id/arrow"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/arrow_shape"
android:rotation="270" />
<RelativeLayout
android:id="#+id/rectangle"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="#drawable/rectangle_shape"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/tools" />
</LinearLayout>
A solution with a gradient:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:width="2dp" android:height="2dp"
android:top="4dp" android:left="3dp">
<rotate android:fromDegrees="45"
android:pivotX="0"
android:pivotY="0">
<shape>
<solid android:color="#D83945"/>
</shape>
</rotate>
</item>
<item android:width="50dp" android:height="11dp" android:left="3dp">
<shape>
<gradient
android:startColor="#D83945"
android:endColor="#F9700C"
/>
<corners
android:radius="2dp"
/>
</shape>
</item>
</layer-list>
The final result is:

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>

Android - dynamically change the shape file attributes

I have a shape.xml file something like this :
<?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="-45" >
<shape android:shape="rectangle" >
<stroke
android:layout_width="match_parent"
android:color="#color/red" />
<solid android:color="#color/red" />
</shape>
</rotate>
</item>
</layer-list>
I have to change the attributes of this file dynamically. I know that i have to use GradientDrawable but i am not be able to get the exact configuration as defined in this file. Can anyone help.

Categories

Resources