Thursday, 17 January 2013

GradientDrawable in Android

A Drawable with a color gradient for buttons, backgrounds, etc.
It can be defined in an XML file with the <shape> element. For more information, see the guide to Drawable Resources.



<?xml version="1.0"encoding="utf-8"?>
<gradient
    android:angle="-90"
    android:endColor="#A71C1C"
    android:startColor="#7c0000"
    android:type="radial" />



Gradient is used in shape and it looks like this.

<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="90"
        android:endColor="#A71C1C"
        android:startColor="#7c0000" />

</shape> 

another example of shape drawable: 
 

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />

    <gradient
        android:angle="225"
        android:endColor="#DD2ECCFA"
        android:startColor="#DD000000" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>

Attributes Name and its meaning for gradient drawable in android :

android:type

There are 3 types of gradients, the default and the one for this question is "linear". The other 2 are "radial" and "sweep".

android:angle

Counter-clockwise rotation of the gradient, where 0 is | start color --> end color | (horizontally).

android:startColor

Color the gradient starts from, start is defined by the rotation.

android:endColor

Color the gradient ends with, end is defined by the rotation.

android:centerColor

There can also be a color in between the start and end color, if desired.

 We can use gradient in selector also.



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <gradient android:angle="270" android:endColor="@color/button_pressed" android:startColor="@color/button_pressed" />

            <stroke android:width="3dp" android:color="@color/button_pressed" />

            <corners android:radius="3dp" />

            <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
        </shape></item>
    <item android:state_focused="true"><shape>
            <gradient android:angle="270" android:endColor="@color/info_normal" android:startColor="@color/info_normal" />

            <stroke android:width="3dp" android:color="@color/info_normal" />

            <corners android:radius="3dp" />

            <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
        </shape></item>
    <item><shape>
            <gradient android:angle="270" android:endColor="@color/info_normal" android:startColor="@color/info_normal" />

            <stroke android:width="3dp" android:color="@color/info_normal" />

            <corners android:radius="3dp" />

            <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
        </shape></item>

</selector>



One more thing is useful for shape called shape type"
there are four types of shape available in android.
 
rectangle A rectangle that fills the containing View. This is the default shape.
oval An oval shape that fits the dimensions of the containing View.
line A horizontal line that spans the width of the containing View. This shape requires the <stroke> element to define the width of the line.
ring A ring shape.
  
 
Our new android application for EGreeting.Have a look to it.
 The Greetings Android Application 

Download:-

 



NovaRadix Android Application Development Team
NovaRadix Technology
WWW.NOVARADIX.COM


 

1 comment:

Call web services easily

get source code from here, Download