Skip to content

Most visited

Recently visited

navigation
Added in API level 1

GradientDrawable

public class GradientDrawable
extends Drawable

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


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

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.

ColorStateList getColor()

Returns the color state list used to fill the shape, or null if the shape is filled with a gradient or has no fill color.

ColorFilter getColorFilter()

Returns the current color filter, or null if none set.

int[] getColors()

Returns the colors used to draw the gradient, or null if the gradient is drawn using a single color or no colors.

Drawable.ConstantState getConstantState()

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

float[] getCornerRadii()

Returns the radii for each of the 4 corners.

float getCornerRadius()

Returns the radius for the corners of the gradient.

float getGradientCenterX()

Returns the center X location of this gradient in pixels.

float getGradientCenterY()

Returns the center Y location of this gradient in pixels.

float getGradientRadius()

Returns the radius of the gradient in pixels.

int getGradientType()

Returns the type of gradient used by this drawable, one of LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT.

int getIntrinsicHeight()

Returns the drawable's intrinsic height.

int getIntrinsicWidth()

Returns the drawable's intrinsic width.

int getOpacity()

Return the opacity/transparency of this Drawable.

GradientDrawable.Orientation getOrientation()

Returns the orientation of the gradient defined in this drawable.

void getOutline(Outline outline)

Called to get the drawable to populate the Outline that defines its drawing area.

boolean getPadding(Rect padding)

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.

int getShape()

Returns the type of shape used by this drawable, one of LINE, OVAL, RECTANGLE or RING.

boolean getUseLevel()

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

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(ColorStateList colorStateList)

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

void setColor(int argb)

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

void setColorFilter(ColorFilter colorFilter)

Specify an optional color filter for the drawable.

void setColors(int[] colors)

Sets the colors used to draw the gradient.

void setCornerRadii(float[] radii)

Specifies radii for each of the 4 corners.

void setCornerRadius(float radius)

Specifies the radius for the corners of the gradient.

void setDither(boolean dither)

This method is deprecated. This property is ignored.

void setGradientCenter(float x, float y)

Sets the center location in pixels 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)

Sets 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, ColorStateList colorStateList, float dashWidth, float dashGap)

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, int color)

Set the stroke width and color for the drawable.

void setTintList(ColorStateList tint)

Specifies tint color for this drawable as a color state list.

void setTintMode(PorterDuff.Mode tintMode)

Specifies a tint blending mode for this 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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

android:gradientRadius

Radius of the gradient, used only with radial gradient.

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

May 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).

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

android:useLevel

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.

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.

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.

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.

Constants

LINE

Added in API level 1
int LINE

Shape is a line

Constant Value: 2 (0x00000002)

LINEAR_GRADIENT

Added in API level 1
int LINEAR_GRADIENT

Gradient is linear (default.)

Constant Value: 0 (0x00000000)

OVAL

Added in API level 1
int OVAL

Shape is an ellipse

Constant Value: 1 (0x00000001)

RADIAL_GRADIENT

Added in API level 1
int RADIAL_GRADIENT

Gradient is circular.

Constant Value: 1 (0x00000001)

RECTANGLE

Added in API level 1
int RECTANGLE

Shape is a rectangle, possibly with rounded corners

Constant Value: 0 (0x00000000)

RING

Added in API level 1
int RING

Shape is a ring.

Constant Value: 3 (0x00000003)

SWEEP_GRADIENT

Added in API level 1
int SWEEP_GRADIENT

Gradient is a sweep.

Constant Value: 2 (0x00000002)

Public constructors

GradientDrawable

Added in API level 1
GradientDrawable ()

GradientDrawable

Added in API level 1
GradientDrawable (GradientDrawable.Orientation orientation, 
                int[] colors)

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

Parameters
orientation GradientDrawable.Orientation
colors int

Public methods

applyTheme

Added in API level 21
void applyTheme (Resources.Theme t)

Applies the specified theme to this Drawable and its children.

Parameters
t Resources.Theme: the theme to apply

canApplyTheme

Added in API level 21
boolean canApplyTheme ()

Returns
boolean

draw

Added in API level 1
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).

Parameters
canvas Canvas: The canvas to draw into

getAlpha

Added in API level 19
int getAlpha ()

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.

Returns
int

getChangingConfigurations

Added in API level 1
int getChangingConfigurations ()

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
int Returns a mask of the changing configuration parameters, as defined by ActivityInfo.

getColor

Added in API level 24
ColorStateList getColor ()

Returns the color state list used to fill the shape, or null if the shape is filled with a gradient or has no fill color.

Returns
ColorStateList the color state list used to fill this gradient, or null

See also:

getColorFilter

Added in API level 21
ColorFilter getColorFilter ()

Returns the current color filter, or null if none set.

Returns
ColorFilter the current color filter, or null if none set

getColors

Added in API level 24
int[] getColors ()

Returns the colors used to draw the gradient, or null if the gradient is drawn using a single color or no colors.

Returns
int[] the colors used to draw the gradient, or null

See also:

getConstantState

Added in API level 1
Drawable.ConstantState getConstantState ()

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

Returns
Drawable.ConstantState The ConstantState associated to that Drawable.

getCornerRadii

Added in API level 24
float[] getCornerRadii ()

Returns the 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.

If the radius was previously set with setCornerRadius(float), or if the corners are not rounded, this method will return null.

Returns
float[] an array containing the radii for each of the 4 corners, or null

See also:

getCornerRadius

Added in API level 24
float getCornerRadius ()

Returns the radius for the corners of the gradient.

If the radius was previously set with setCornerRadii(float[]), or if the corners are not rounded, this method will return null.

Returns
float the radius in pixels of the corners of the rectangle shape, or 0

See also:

getGradientCenterX

Added in API level 24
float getGradientCenterX ()

Returns the center X location of this gradient in pixels.

Returns
float the center X location of this gradient in pixels

See also:

getGradientCenterY

Added in API level 24
float getGradientCenterY ()

Returns the center Y location of this gradient in pixels.

Returns
float the center Y location of this gradient in pixels

See also:

getGradientRadius

Added in API level 21
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
float the radius of the gradient in pixels

See also:

getGradientType

Added in API level 24
int getGradientType ()

Returns the type of gradient used by this drawable, one of LINEAR_GRADIENT, RADIAL_GRADIENT, or SWEEP_GRADIENT.

Returns
int the type of gradient used by this drawable

See also:

getIntrinsicHeight

Added in API level 1
int getIntrinsicHeight ()

Returns the drawable's intrinsic height.

Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.

Returns
int the intrinsic height, or -1 if no intrinsic height

getIntrinsicWidth

Added in API level 1
int getIntrinsicWidth ()

Returns the drawable's intrinsic width.

Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.

Returns
int the intrinsic width, or -1 if no intrinsic width

getOpacity

Added in API level 1
int getOpacity ()

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

An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.

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 necessarily take into account a custom alpha or color filter that has been applied by the client through the setAlpha(int) or setColorFilter(ColorFilter) methods. Some subclasses, such as BitmapDrawable, ColorDrawable, and GradientDrawable, do account for the value of setAlpha(int), but the general behavior is dependent upon the implementation of the subclass.

Returns
int int The opacity class of the Drawable.

getOrientation

Added in API level 16
GradientDrawable.Orientation getOrientation ()

Returns the orientation of the gradient defined in this drawable.

Returns
GradientDrawable.Orientation the orientation of the gradient defined in this drawable

See also:

getOutline

Added in API level 21
void getOutline (Outline outline)

Called to get the drawable to populate the Outline that defines its drawing area.

This method is called by the default ViewOutlineProvider to define the outline of the View.

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

Parameters
outline Outline

getPadding

Added in API level 1
boolean getPadding (Rect padding)

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).

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

getShape

Added in API level 24
int getShape ()

Returns the type of shape used by this drawable, one of LINE, OVAL, RECTANGLE or RING.

Returns
int the type of shape used by this drawable

See also:

getUseLevel

Added in API level 24
boolean getUseLevel ()

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

Returns
boolean true if this drawable should honor its level, false otherwise

See also:

inflate

Added in API level 21
void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: Resources used to resolve attribute values
parser XmlPullParser: XML parser from which to inflate this Drawable
attrs AttributeSet: Base set of attribute values
theme Resources.Theme: Theme to apply, may be null
Throws
XmlPullParserException
IOException

isStateful

Added in API level 1
boolean isStateful ()

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
boolean True if this drawable changes its appearance based on state, false otherwise.

mutate

Added in API level 3
Drawable mutate ()

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
Drawable This drawable.

setAlpha

Added in API level 1
void setAlpha (int alpha)

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

Parameters
alpha int

setColor

Added in API level 21
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 ColorStateList: The color state list used to fill the shape

See also:

setColor

Added in API level 1
void setColor (int argb)

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 int: The color used to fill the shape

See also:

setColorFilter

Added in API level 1
void setColorFilter (ColorFilter colorFilter)

Specify an optional color filter for the drawable.

If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.

Pass null to remove any existing color filter.

Note: Setting a non-null color filter disables tint.

Parameters
colorFilter ColorFilter: The color filter to apply, or null to remove the existing color filter

setColors

Added in API level 16
void setColors (int[] colors)

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 colors will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the colors.

Parameters
colors int: an array containing 2 or more ARGB colors

See also:

setCornerRadii

Added in API level 1
void setCornerRadii (float[] radii)

Specifies 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 float: an array of length >= 8 containing 4 pairs of X and Y radius for each corner, specified in pixels

See also:

setCornerRadius

Added in API level 1
void setCornerRadius (float radius)

Specifies the 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 float: The radius in pixels of the corners of the rectangle shape

See also:

setDither

Added in API level 1
void setDither (boolean dither)

This method is deprecated.
This property is ignored.

Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.

Parameters
dither boolean

setGradientCenter

Added in API level 1
void setGradientCenter (float x, 
                float y)

Sets the center location in pixels 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 float: the x coordinate of the gradient's center in pixels
y float: the y coordinate of the gradient's center in pixels

See also:

setGradientRadius

Added in API level 1
void setGradientRadius (float gradientRadius)

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 float: the radius of the gradient in pixels

See also:

setGradientType

Added in API level 1
void setGradientType (int gradient)

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 int: The type of the gradient: LINEAR_GRADIENT, RADIAL_GRADIENT or SWEEP_GRADIENT

See also:

setOrientation

Added in API level 16
void setOrientation (GradientDrawable.Orientation orientation)

Sets 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 GradientDrawable.Orientation: the desired orientation (angle) of the gradient

See also:

setShape

Added in API level 1
void setShape (int shape)

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 int: The desired shape for this drawable: LINE, OVAL, RECTANGLE or RING

See also:

setSize

Added in API level 1
void setSize (int width, 
                int height)

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 int: The width of the shape used by this drawable
height int: The height of the shape used by this drawable

See also:

setStroke

Added in API level 21
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 int: The width in pixels of the stroke
colorStateList ColorStateList: The color state list of the stroke

See also:

setStroke

Added in API level 21
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 int: The width in pixels of the stroke
colorStateList ColorStateList: The color state list of the stroke
dashWidth float: The length in pixels of the dashes, set to 0 to disable dashes
dashGap float: The gap in pixels between dashes

See also:

setStroke

Added in API level 1
void setStroke (int width, 
                int color, 
                float dashWidth, 
                float dashGap)

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 int: The width in pixels of the stroke
color int: The color of the stroke
dashWidth float: The length in pixels of the dashes, set to 0 to disable dashes
dashGap float: The gap in pixels between dashes

See also:

setStroke

Added in API level 1
void setStroke (int width, 
                int color)

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 int: The width in pixels of the stroke
color int: The color of the stroke

See also:

setTintList

Added in API level 21
void setTintList (ColorStateList tint)

Specifies tint color for this drawable as a color state list.

A Drawable's drawing content will be blended together with its tint before it is drawn to the screen. This functions similarly to setColorFilter(int, PorterDuff.Mode).

Note: Setting a color filter via setColorFilter(ColorFilter) or setColorFilter(int, PorterDuff.Mode) overrides tint.

Parameters
tint ColorStateList: Color state list to use for tinting this drawable, or null to clear the tint

setTintMode

Added in API level 21
void setTintMode (PorterDuff.Mode tintMode)

Specifies a tint blending mode for this drawable.

Defines how this drawable's tint color should be blended into the drawable before it is drawn to screen. Default tint mode is SRC_IN.

Note: Setting a color filter via setColorFilter(ColorFilter) or setColorFilter(int, PorterDuff.Mode) overrides tint.

Parameters
tintMode PorterDuff.Mode: A Porter-Duff blending mode

setUseLevel

Added in API level 1
void setUseLevel (boolean useLevel)

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 boolean: true if this drawable should honor its level, false otherwise

See also:

Protected methods

onBoundsChange

Added in API level 1
void onBoundsChange (Rect r)

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

Parameters
r Rect

onLevelChange

Added in API level 1
boolean onLevelChange (int level)

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

Parameters
level int
Returns
boolean 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.

onStateChange

Added in API level 1
boolean onStateChange (int[] stateSet)

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

Parameters
stateSet int
Returns
boolean 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.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.