Android APIs
public class

GradientDrawable

extends Drawable
java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.GradientDrawable

Class Overview

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.

Summary

Nested Classes
enum GradientDrawable.Orientation Controls how the gradient is oriented relative to the drawable's bounds  
XML Attributes
Attribute Name Related Method Description
android:angle Angle of the gradient. 
android:bottom Amount of bottom padding inside the gradient shape. 
android:centerColor Optional center color. 
android:centerX X coordinate of the origin of the gradient within the shape. 
android:centerY Y coordinate of the origin of the gradient within the shape. 
android:color Solid color for the gradient shape. 
android:color Color of the gradient shape's stroke. 
android:dashGap Gap between dashes in the stroke. 
android:dashWidth Length of a dash in the stroke. 
android:endColor End color of the gradient. 
android:gradientRadius Radius of the gradient, used only with radial gradient. 
android:height Height of the gradient shape. 
android:innerRadius Inner radius of the ring. 
android:innerRadiusRatio Inner radius of the ring expressed as a ratio of the ring's width. 
android:left Amount of left padding inside the gradient shape. 
android:right Amount of right padding inside the gradient shape. 
android:shape Indicates what shape to fill with a gradient. 
android:startColor Start color of the gradient. 
android:thickness Thickness of the ring. 
android:thicknessRatio Thickness of the ring expressed as a ratio of the ring's width. 
android:top Amount of top padding inside the gradient shape. 
android:type Type of gradient. 
android:useLevel  
android:useLevel Indicates whether the drawable's level affects the way the gradient is drawn. 
android:visible Indicates whether the drawable should intially be visible. 
android:width Width of the gradient shape. 
android:width Width of the gradient shape's stroke. 
Constants
int LINE Shape is a line
int LINEAR_GRADIENT Gradient is linear (default.)
int OVAL Shape is an ellipse
int RADIAL_GRADIENT Gradient is circular.
int RECTANGLE Shape is a rectangle, possibly with rounded corners
int RING Shape is a ring.
int SWEEP_GRADIENT Gradient is a sweep.
Public Constructors
GradientDrawable()
GradientDrawable(GradientDrawable.Orientation orientation, int[] colors)
Create a new gradient drawable given an orientation and an array of colors for the gradient.
Public Methods
void applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
boolean canApplyTheme()
void draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
int getAlpha()
Gets the current alpha value for the drawable.
int getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.
Drawable.ConstantState getConstantState()
Return a Drawable.ConstantState instance that holds the shared state of this Drawable.
float getGradientRadius()
Returns the radius of the gradient in pixels.
int getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
int getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
int getOpacity()
Return the opacity/transparency of this Drawable.
GradientDrawable.Orientation getOrientation()
Returns the orientation of the gradient defined in this drawable.
boolean getOutline(Outline outline)
Called to get the drawable to populate the Outline.
boolean getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
boolean isStateful()
Indicates whether this drawable will change its appearance based on state.
Drawable mutate()
Make this drawable mutable.
void setAlpha(int alpha)
Specify an alpha value for the drawable.
void setColor(int argb)

Changes this drawable to use a single color instead of a gradient.

void setColor(ColorStateList colorStateList)
Changes this drawable to use a single color state list instead of a gradient.
void setColorFilter(ColorFilter cf)
Specify an optional color filter for the drawable.
void setColors(int[] colors)

Sets the colors used to draw the gradient.

void setCornerRadii(float[] radii)

Specify radii for each of the 4 corners.

void setCornerRadius(float radius)

Specify radius for the corners of the gradient.

void setDither(boolean dither)
Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.
void setGradientCenter(float x, float y)

Sets the center location of the gradient.

void setGradientRadius(float gradientRadius)

Sets the radius of the gradient.

void setGradientType(int gradient)

Sets the type of gradient used by this drawable..

void setOrientation(GradientDrawable.Orientation orientation)

Changes the orientation of the gradient defined in this drawable.

void setShape(int shape)

Sets the type of shape used to draw the gradient.

void setSize(int width, int height)

Sets the size of the shape drawn by this drawable.

void setStroke(int width, ColorStateList colorStateList)

Set the stroke width and color state list for the drawable.

void setStroke(int width, int color, float dashWidth, float dashGap)

Set the stroke width and color for the drawable.

void setStroke(int width, ColorStateList colorStateList, float dashWidth, float dashGap)

Set the stroke width and color state list for the drawable.

void setStroke(int width, int color)

Set the stroke width and color for the drawable.

void setUseLevel(boolean useLevel)

Sets whether or not this drawable will honor its level property.

Protected Methods
void onBoundsChange(Rect r)
Override this in your subclass to change appearance if you vary based on the bounds.
boolean onLevelChange(int level)
Override this in your subclass to change appearance if you vary based on level.
boolean onStateChange(int[] stateSet)
Override this in your subclass to change appearance if you recognize the specified state.
[Expand]
Inherited Methods
From class android.graphics.drawable.Drawable
From class java.lang.Object

XML Attributes

android:angle

Angle of the gradient.

Must be a floating point value, such as "1.2".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol angle.

Related Methods

android:bottom

Amount of bottom padding inside the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol bottom.

Related Methods

android:centerColor

Optional center color. For linear gradients, use centerX or centerY to place the center color.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol centerColor.

Related Methods

android:centerX

X coordinate of the origin of the gradient within the shape.

May be a floating point value, such as "1.2".

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol centerX.

Related Methods

android:centerY

Y coordinate of the origin of the gradient within the shape.

May be a floating point value, such as "1.2".

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol centerY.

Related Methods

android:color

Solid color for the gradient shape.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol color.

Related Methods

android:color

Color of the gradient shape's stroke.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol color.

Related Methods

android:dashGap

Gap between dashes in the stroke.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol dashGap.

Related Methods

android:dashWidth

Length of a dash in the stroke.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol dashWidth.

Related Methods

android:endColor

End color of the gradient.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol endColor.

Related Methods

android:gradientRadius

Radius of the gradient, used only with radial gradient.

May be a floating point value, such as "1.2".

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol gradientRadius.

Related Methods

android:height

Height of the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol height.

Related Methods

android:innerRadius

Inner radius of the ring. When defined, innerRadiusRatio is ignored.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol innerRadius.

Related Methods

android:innerRadiusRatio

Inner radius of the ring expressed as a ratio of the ring's width. For instance, if innerRadiusRatio=9, then the inner radius equals the ring's width divided by 9. This value is ignored if innerRadius is defined. Default value is 9.

Must be a floating point value, such as "1.2".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol innerRadiusRatio.

Related Methods

android:left

Amount of left padding inside the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol left.

Related Methods

android:right

Amount of right padding inside the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol right.

Related Methods

android:shape

Indicates what shape to fill with a gradient.

Must be one of the following constant values.

ConstantValueDescription
rectangle0 Rectangle shape, with optional rounder corners.
oval1 Oval shape.
line2 Line shape.
ring3 Ring shape.

This corresponds to the global attribute resource symbol shape.

Related Methods

android:startColor

Start color of the gradient.

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol startColor.

Related Methods

android:thickness

Thickness of the ring. When defined, thicknessRatio is ignored.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol thickness.

Related Methods

android:thicknessRatio

Thickness of the ring expressed as a ratio of the ring's width. For instance, if thicknessRatio=3, then the thickness equals the ring's width divided by 3. This value is ignored if innerRadius is defined. Default value is 3.

Must be a floating point value, such as "1.2".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol thicknessRatio.

Related Methods

android:top

Amount of top padding inside the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol top.

Related Methods

android:type

Type of gradient. The default type is linear.

Must be one of the following constant values.

ConstantValueDescription
linear0 Linear gradient.
radial1 Radial, or circular, gradient.
sweep2 Sweep, or angled or diamond, gradient.

This corresponds to the global attribute resource symbol type.

Related Methods

android:useLevel

Related Methods

android:useLevel

Indicates whether the drawable's level affects the way the gradient is drawn.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol useLevel.

Related Methods

android:visible

Indicates whether the drawable should intially be visible.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol visible.

Related Methods

android:width

Width of the gradient shape.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol width.

Related Methods

android:width

Width of the gradient shape's stroke.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol width.

Related Methods

Constants

public static final int LINE

Added in API level 1

Shape is a line

Constant Value: 2 (0x00000002)

public static final int LINEAR_GRADIENT

Added in API level 1

Gradient is linear (default.)

Constant Value: 0 (0x00000000)

public static final int OVAL

Added in API level 1

Shape is an ellipse

Constant Value: 1 (0x00000001)

public static final int RADIAL_GRADIENT

Added in API level 1

Gradient is circular.

Constant Value: 1 (0x00000001)

public static final int RECTANGLE

Added in API level 1

Shape is a rectangle, possibly with rounded corners

Constant Value: 0 (0x00000000)

public static final int RING

Added in API level 1

Shape is a ring.

Constant Value: 3 (0x00000003)

public static final int SWEEP_GRADIENT

Added in API level 1

Gradient is a sweep.

Constant Value: 2 (0x00000002)

Public Constructors

public GradientDrawable ()

Added in API level 1

public GradientDrawable (GradientDrawable.Orientation orientation, int[] colors)

Added in API level 1

Create a new gradient drawable given an orientation and an array of colors for the gradient.

Public Methods

public void applyTheme (Resources.Theme t)

Applies the specified theme to this Drawable and its children.

public boolean canApplyTheme ()

public void draw (Canvas canvas)

Added in API level 1

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Parameters
canvas The canvas to draw into

public int getAlpha ()

Added in API level 19

Gets the current alpha value for the drawable. 0 means fully transparent, 255 means fully opaque. This method is implemented by Drawable subclasses and the value returned is specific to how that class treats alpha. The default return value is 255 if the class does not override this method to return a value specific to its use of alpha.

public int getChangingConfigurations ()

Added in API level 1

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created. The default implementation returns whatever was provided through setChangingConfigurations(int) or 0 by default. Subclasses may extend this to or in the changing configurations of any other drawables they hold.

Returns
  • Returns a mask of the changing configuration parameters, as defined by Configuration.

public Drawable.ConstantState getConstantState ()

Added in API level 1

Return a Drawable.ConstantState instance that holds the shared state of this Drawable.

Returns
  • The ConstantState associated to that Drawable.

public float getGradientRadius ()

Returns the radius of the gradient in pixels. The radius is valid only when the gradient type is set to RADIAL_GRADIENT.

Returns
  • Radius in pixels.

public int getIntrinsicHeight ()

Added in API level 1

Return the intrinsic height of the underlying drawable object. Returns -1 if it has no intrinsic height, such as with a solid color.

public int getIntrinsicWidth ()

Added in API level 1

Return the intrinsic width of the underlying drawable object. Returns -1 if it has no intrinsic width, such as with a solid color.

public int getOpacity ()

Added in API level 1

Return the opacity/transparency of this Drawable. The returned value is one of the abstract format constants in PixelFormat: UNKNOWN, TRANSLUCENT, TRANSPARENT, or OPAQUE.

Generally a Drawable should be as conservative as possible with the value it returns. For example, if it contains multiple child drawables and only shows one of them at a time, if only one of the children is TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be returned. You can use the method resolveOpacity(int, int) to perform a standard reduction of two opacities to the appropriate single output.

Note that the returned value does not take into account a custom alpha or color filter that has been applied by the client through the setAlpha(int) or setColorFilter(ColorFilter) methods.

Returns
  • int The opacity class of the Drawable.

public GradientDrawable.Orientation getOrientation ()

Added in API level 16

Returns the orientation of the gradient defined in this drawable.

public boolean getOutline (Outline outline)

Called to get the drawable to populate the Outline.

This method will be called by a View on its background Drawable after bounds change, or its Drawable is invalidated, if the View's Outline isn't set explicitly. This allows the background Drawable to define the shape of the shadow cast by the View.

The default behavior defines the outline to be the bounding rectangle. Subclasses that wish to convey a different shape must override this method.

Returns
  • true if this drawable actually has an outline, else false. The outline must be populated by the drawable if true is returned.

public boolean getPadding (Rect padding)

Added in API level 1

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds. Positive values move toward the center of the Drawable (set Rect.inset).

Returns
  • true if this drawable actually has a padding, else false. When false is returned, the padding is always set to 0.

public void inflate (Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

Parameters
r Resources used to resolve attribute values
parser XML parser from which to inflate this Drawable
attrs Base set of attribute values
theme Theme to apply, may be null

public boolean isStateful ()

Added in API level 1

Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.

Returns
  • True if this drawable changes its appearance based on state, false otherwise.

public Drawable mutate ()

Added in API level 3

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.

Returns
  • This drawable.

public void setAlpha (int alpha)

Added in API level 1

Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.

public void setColor (int argb)

Added in API level 1

Changes this drawable to use a single color instead of a gradient.

Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

Parameters
argb The color used to fill the shape

public void setColor (ColorStateList colorStateList)

Changes this drawable to use a single color state list instead of a gradient. Calling this method with a null argument will clear the color and is equivalent to calling setColor(int) with the argument TRANSPARENT.

Note: changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

Parameters
colorStateList The color state list used to fill the shape
See Also

public void setColorFilter (ColorFilter cf)

Added in API level 1

Specify an optional color filter for the drawable. Pass null to remove any existing color filter.

Parameters
cf the color filter to apply, or null to remove the existing color filter

public void setColors (int[] colors)

Added in API level 16

Sets the colors used to draw the gradient. Each color is specified as an ARGB integer and the array must contain at least 2 colors.

Note: changing orientation will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the orientation.

Parameters
colors 2 or more ARGB colors

public void setCornerRadii (float[] radii)

Added in API level 1

Specify radii for each of the 4 corners. For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
radii 4 pairs of X and Y radius for each corner, specified in pixels. The length of this array must be >= 8

public void setCornerRadius (float radius)

Added in API level 1

Specify radius for the corners of the gradient. If this is > 0, then the drawable is drawn in a round-rectangle, rather than a rectangle. This property is honored only when the shape is of type RECTANGLE.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
radius The radius in pixels of the corners of the rectangle shape

public void setDither (boolean dither)

Added in API level 1

Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component. This can improve the look on those devices, but can also slow down the drawing a little.

public void setGradientCenter (float x, float y)

Added in API level 1

Sets the center location of the gradient. The radius is honored only when the gradient type is set to RADIAL_GRADIENT or SWEEP_GRADIENT.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
x The x coordinate of the gradient's center
y The y coordinate of the gradient's center

public void setGradientRadius (float gradientRadius)

Added in API level 1

Sets the radius of the gradient. The radius is honored only when the gradient type is set to RADIAL_GRADIENT.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
gradientRadius The radius of the gradient in pixels

public void setGradientType (int gradient)

Added in API level 1

Sets the type of gradient used by this drawable..

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
gradient The type of the gradient: LINEAR_GRADIENT, RADIAL_GRADIENT or SWEEP_GRADIENT
See Also

public void setOrientation (GradientDrawable.Orientation orientation)

Added in API level 16

Changes the orientation of the gradient defined in this drawable.

Note: changing orientation will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the orientation.

Parameters
orientation The desired orientation (angle) of the gradient
See Also

public void setShape (int shape)

Added in API level 1

Sets the type of shape used to draw the gradient.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
shape The desired shape for this drawable: LINE, OVAL, RECTANGLE or RING
See Also

public void setSize (int width, int height)

Added in API level 1

Sets the size of the shape drawn by this drawable.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width The width of the shape used by this drawable
height The height of the shape used by this drawable

public void setStroke (int width, ColorStateList colorStateList)

Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width The width in pixels of the stroke
colorStateList The color state list of the stroke

public void setStroke (int width, int color, float dashWidth, float dashGap)

Added in API level 1

Set the stroke width and color for the drawable. If width is zero, then no stroke is drawn. This method can also be used to dash the stroke.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width The width in pixels of the stroke
color The color of the stroke
dashWidth The length in pixels of the dashes, set to 0 to disable dashes
dashGap The gap in pixels between dashes

public void setStroke (int width, ColorStateList colorStateList, float dashWidth, float dashGap)

Set the stroke width and color state list for the drawable. If width is zero, then no stroke is drawn. This method can also be used to dash the stroke.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width The width in pixels of the stroke
colorStateList The color state list of the stroke
dashWidth The length in pixels of the dashes, set to 0 to disable dashes
dashGap The gap in pixels between dashes

public void setStroke (int width, int color)

Added in API level 1

Set the stroke width and color for the drawable. If width is zero, then no stroke is drawn.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
width The width in pixels of the stroke
color The color of the stroke

public void setUseLevel (boolean useLevel)

Added in API level 1

Sets whether or not this drawable will honor its level property.

Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property.

Parameters
useLevel True if this drawable should honor its level, false otherwise

Protected Methods

protected void onBoundsChange (Rect r)

Added in API level 1

Override this in your subclass to change appearance if you vary based on the bounds.

protected boolean onLevelChange (int level)

Added in API level 1

Override this in your subclass to change appearance if you vary based on level.

Returns
  • Returns true if the level change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last level.

protected boolean onStateChange (int[] stateSet)

Added in API level 1

Override this in your subclass to change appearance if you recognize the specified state.

Returns
  • Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.