Android - dynamically change the shape file attributes - android

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.

Related

Android rectangle shape with two different color

how can I create rectangle shape by using two different colors with shadow? like above image.
Please create a drawable file and put the below code in it.
<?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:height="20dp" />
<solid android:color="#F66D08" />
</shape>
</item>
<item android:top="50dp">
<shape android:shape="rectangle" >
<gradient android:endColor="#AD1B1D"
android:startColor="#E2360A"
android:angle="270" />
</shape>
</item>
</layer-list>
layer-list can be used to solve this
<?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="40dp"
android:height="40dp" />
<solid android:color="#F86F05" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#B31F19" />
</shape>
</item>
</layer-list>
And the screenshot of the same.
If you want gradient instead of solid color, change solid to gradient and set startColor, endColor and angle.
Place these code in your activity_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:gravity="center"
android:background="#F66D08"
android:layout_width="match_parent"
android:layout_height="20dp">
</LinearLayout>
<LinearLayout
android:background="#drawable/introslidergradiant"
android:layout_width="match_parent"
android:layout_height="100dp"></LinearLayout>
</LinearLayout>
And create drawble res file with introslidergradiant.xml name :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#A31720"
android:endColor="#E1350B"
android:type="linear" />
</shape>
</item>
</selector>
You have to make a xml file in the drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:startColor="#FFFF820D"
android:endColor="#FFA32815"
android:angle="90"/>
</shape>
use the following code or you can generate your own gradient here just set the color and select the android option and it will generate the gradient for you .

Make a view with rectangle shape using xml definitions (drawable) in Android

https://drive.google.com/file/d/0B0vP0ppNf8G3MFdBRmF5WkJEZ3c/view?usp=sharing
I want to create shape as above.
How do you create this using xml?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:right="-100dp">
<rotate
android:fromDegrees="-5"
android:pivotX="0%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="#f9c" />
</shape>
</rotate>
</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>

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 : Change Shape Color in XML Dynamically

I applied this custom-xml as TextView background.
It working as I expect. However, There are two shapes in this xml and I want to change color of these two shapes dynamically.
After searched, I didn't find any solution to solve this issue.
Any Idea? please advice, Thank.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="0"
android:toDegrees="0"
android:pivotX="0%"
android:pivotY="0%" >
<shape
android:shape="rectangle" >
<solid android:color="#ff0000" />
</shape>
</rotate>
</item>
<item>
<rotate
android:fromDegrees="-405"
android:toDegrees="0"
android:pivotX="0%"
android:pivotY="100%" >
<shape
android:shape="rectangle" >
<solid android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
If you want to change color just once then you can have another custom-xml with the same shapes but with new color values and set the background of the textview to that xml dynamically.

Categories

Resources