Added in API level 1

GradientDrawable


open class GradientDrawable : Drawable
kotlin.Any
   ↳ 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

Controls how the gradient is oriented relative to the drawable's bounds

XML attributes
android:angle Angle of the gradient, used only with linear gradient.
android:bottom Amount of bottom padding inside the gradient shape.
android:centerColor Optional center color.
android:centerX X-position of the center point of the gradient within the shape as a fraction of the width.
android:centerY Y-position of the center point of the gradient within the shape as a fraction of the height.
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 Whether the drawable level value (see android.graphics.drawable.Drawable#getLevel() ) is used to scale the shape.
android:useLevel Whether the drawable level value (see android.graphics.drawable.Drawable#getLevel() ) is used to scale the gradient.
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
static Int

Shape is a line

static Int

Gradient is linear (default.)

static Int

Shape is an ellipse

static Int

Gradient is circular.

static Int

Shape is a rectangle, possibly with rounded corners

static Int

Shape is a ring.

static Int

Gradient is a sweep.

Public constructors

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

Public methods
open Unit

Applies the specified theme to this Drawable and its children.

open Boolean

open Unit
draw(canvas: Canvas)

open Int

open Int

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

open ColorStateList?

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.

open ColorFilter?

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

open IntArray?

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

open Drawable.ConstantState?

open FloatArray?

Returns the radii for each of the 4 corners.

open Float

Returns the radius for the corners of the gradient, that was previously set with setCornerRadius(float).

open Float

Returns the X-position of the center of the gradient as a fraction of the width.

open Float

Returns the Y-position of the center of this gradient as a fraction of the height.

open Float

Returns the radius of the gradient in pixels.

open Int

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

open Int

Return the inner radius of the ring
The units of this value are pixels.

open Float

Return the inner radius of the ring expressed as a ratio of the ring's width.

open Int

open Int

open Int

open Insets

open GradientDrawable.Orientation!

Returns the orientation of the gradient defined in this drawable.

open Unit
getOutline(outline: Outline)

open Boolean
getPadding(padding: Rect)

open Int

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

open Int

Return the thickness of the ring
The units of this value are pixels.

open Float

Return the thickness ratio of the ring expressed as a ratio of the ring's width.

open Boolean

Returns whether this drawable's level property will be used to scale the gradient.

open Boolean

open Unit
inflate(r: Resources, parser: XmlPullParser, attrs: AttributeSet, theme: Resources.Theme?)

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

open Boolean

open Drawable

open Unit
setAlpha(alpha: Int)

open Unit
setColor(argb: Int)

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

open Unit
setColor(colorStateList: ColorStateList?)

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

open Unit
setColorFilter(colorFilter: ColorFilter?)

Specify an optional color filter for the drawable.

open Unit
setColors(colors: IntArray?)

Sets the colors used to draw the gradient.

open Unit
setColors(colors: IntArray?, offsets: FloatArray?)

Sets the colors and offsets used to draw the gradient.

open Unit

Specifies radii for each of the 4 corners.

open Unit

Specifies the radius for the corners of the gradient.

open Unit
setDither(dither: Boolean)

open Unit

Sets the position of the center of the gradient as a fraction of the width and height.

open Unit
setGradientRadius(gradientRadius: Float)

Sets the radius of the gradient.

open Unit
setGradientType(gradient: Int)

Sets the type of gradient used by this drawable.

open Unit
setInnerRadius(innerRadius: Int)

Configure the inner radius of the ring.

open Unit
setInnerRadiusRatio(innerRadiusRatio: Float)

Inner radius of the ring expressed as a ratio of the ring's width.

open Unit

Sets the orientation of the gradient defined in this drawable.

open Unit
setPadding(left: Int, top: Int, right: Int, bottom: Int)

Configure the padding of the gradient shape

open Unit
setShape(shape: Int)

Sets the type of shape used to draw the gradient.

open Unit
setSize(width: Int, height: Int)

Sets the size of the shape drawn by this drawable.

open Unit
setStroke(width: Int, color: Int)

Set the stroke width and color for the drawable.

open Unit
setStroke(width: Int, colorStateList: ColorStateList!)

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

open Unit
setStroke(width: Int, color: Int, dashWidth: Float, dashGap: Float)

Set the stroke width and color for the drawable.

open Unit
setStroke(width: Int, colorStateList: ColorStateList!, dashWidth: Float, dashGap: Float)

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

open Unit
setThickness(thickness: Int)

Configure the thickness of the ring.

open Unit
setThicknessRatio(thicknessRatio: Float)

Configure the thickness of the ring expressed as a ratio of the ring's width.

open Unit

Specifies a tint blending mode for this drawable.

open Unit

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

open Unit
setUseLevel(useLevel: Boolean)

Sets whether this drawable's level property will be used to scale the gradient.

Protected methods
open Unit

open Boolean

open Boolean

Inherited functions

XML attributes

android:angle

android:angle
Angle of the gradient, used only with linear gradient. Must be a multiple of 45 in the range [0, 315].

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

android:bottom

android:bottom
Amount of bottom padding inside the gradient shape.

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), and mm (millimeters).

android:centerColor

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

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

android:centerX

android:centerX
X-position of the center point of the gradient within the shape as a fraction of the width. The default value is 0.5.

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.

android:centerY

android:centerY
Y-position of the center point of the gradient within the shape as a fraction of the height. The default value is 0.5.

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.

android:color

android:color
Solid color for the gradient shape.

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

android:color

android:color
Color of the gradient shape's stroke.

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

android:dashGap

android:dashGap
Gap between dashes in the stroke.

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), and mm (millimeters).

android:dashWidth

android:dashWidth
Length of a dash in the stroke.

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), and mm (millimeters).

android:endColor

android:endColor
End color of the gradient.

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

android:gradientRadius

android:gradientRadius
Radius of the gradient, used only with radial gradient. May be an explicit dimension or a fractional value relative to the shape's minimum dimension.

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

android:height

android:height
Height of the gradient shape.

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), and mm (millimeters).

android:innerRadius

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

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), and mm (millimeters).

android:innerRadiusRatio

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.

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

android:left

android:left
Amount of left padding inside the gradient shape.

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), and mm (millimeters).

android:right

android:right
Amount of right padding inside the gradient shape.

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), and mm (millimeters).

android:shape

android:shape
Indicates what shape to fill with a gradient.

Must be one of the following constant values.

Constant Value Description
line 2 Line shape.
oval 1 Oval shape.
rectangle 0 Rectangle shape, with optional rounder corners.
ring 3 Ring shape.

android:startColor

android:startColor
Start color of the gradient.

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

android:thickness

android:thickness
Thickness of the ring. When defined, thicknessRatio is ignored.

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), and mm (millimeters).

android:thicknessRatio

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.

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

android:top

android:top
Amount of top padding inside the gradient shape.

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), and mm (millimeters).

android:type

android:type
Type of gradient. The default type is linear.

Must be one of the following constant values.

Constant Value Description
linear 0 Linear gradient extending across the center point.
radial 1 Radial gradient extending from the center point outward.
sweep 2 Sweep (or angular) gradient sweeping counter-clockwise around the center point.

android:useLevel

android:useLevel
Whether the drawable level value (see android.graphics.drawable.Drawable#getLevel()) is used to scale the shape. Scaling behavior depends on the shape type. For "ring", the angle is scaled from 0 to 360. For all other types, there is no effect. The default value is true.

May be a boolean value, such as "true" or "false".

android:useLevel

android:useLevel
Whether the drawable level value (see android.graphics.drawable.Drawable#getLevel()) is used to scale the gradient. Scaling behavior varies based on gradient type. For "linear", adjusts the ending position along the gradient's axis of orientation. For "radial", adjusts the outer radius. For "sweep", adjusts the ending angle. The default value is false.

May be a boolean value, such as "true" or "false".

android:visible

android:visible
Indicates whether the drawable should intially be visible.

May be a boolean value, such as "true" or "false".

android:width

android:width
Width of the gradient shape.

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), and mm (millimeters).

android:width

android:width
Width of the gradient shape's stroke.

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), and mm (millimeters).

Constants

LINE

Added in API level 1
static val LINE: Int

Shape is a line

Value: 2

LINEAR_GRADIENT

Added in API level 1
static val LINEAR_GRADIENT: Int

Gradient is linear (default.)

Value: 0

OVAL

Added in API level 1
static val OVAL: Int

Shape is an ellipse

Value: 1

RADIAL_GRADIENT

Added in API level 1
static val RADIAL_GRADIENT: Int

Gradient is circular.

Value: 1

RECTANGLE

Added in API level 1
static val RECTANGLE: Int

Shape is a rectangle, possibly with rounded corners

Value: 0

RING

Added in API level 1
static val RING: Int

Shape is a ring.

Value: 3

SWEEP_GRADIENT

Added in API level 1
static val SWEEP_GRADIENT: Int

Gradient is a sweep.

Value: 2

Public constructors

GradientDrawable

Added in API level 1
GradientDrawable()

GradientDrawable

Added in API level 1
GradientDrawable(
    orientation: GradientDrawable.Orientation!,
    colors: IntArray!)

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

Public methods

applyTheme

Added in API level 21
open fun applyTheme(t: Resources.Theme): Unit

Applies the specified theme to this Drawable and its children.

Parameters
t Resources.Theme: This value cannot be null.

canApplyTheme

Added in API level 21
open fun canApplyTheme(): Boolean

draw

Added in API level 1
open fun draw(canvas: Canvas): Unit
Parameters
canvas Canvas: The canvas to draw into This value cannot be null.

getAlpha

Added in API level 19
open fun getAlpha(): Int
Return
Int Value is between 0 and 255 inclusive

getColor

Added in API level 24
open fun getColor(): ColorStateList?

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.

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

getColorFilter

Added in API level 21
open fun getColorFilter(): ColorFilter?

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

Return
ColorFilter? This value may be null.

getColors

Added in API level 24
open fun getColors(): IntArray?

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

Return
IntArray? the colors used to draw the gradient, or null

getConstantState

Added in API level 1
open fun getConstantState(): Drawable.ConstantState?
Return
Drawable.ConstantState? The ConstantState associated to that Drawable. This value may be null.

getCornerRadii

Added in API level 24
open fun getCornerRadii(): FloatArray?

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.

Return
FloatArray? an array containing the radii for each of the 4 corners, or null

getCornerRadius

Added in API level 24
open fun getCornerRadius(): Float

Returns the radius for the corners of the gradient, that was previously set with setCornerRadius(float).

If the radius was previously cleared via passing null to setCornerRadii(float[]), this method will return 0.

Return
Float the radius in pixels of the corners of the rectangle shape, or 0

See Also

getGradientCenterX

Added in API level 24
open fun getGradientCenterX(): Float

Returns the X-position of the center of the gradient as a fraction of the width.

Return
Float the X-position of the center of the gradient

getGradientCenterY

Added in API level 24
open fun getGradientCenterY(): Float

Returns the Y-position of the center of this gradient as a fraction of the height.

Return
Float the Y-position of the center of the gradient

getGradientRadius

Added in API level 21
open fun getGradientRadius(): Float

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

Return
Float the radius of the gradient in pixels

getGradientType

Added in API level 24
open fun getGradientType(): Int

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

Return
Int the type of gradient used by this drawable Value is android.graphics.drawable.GradientDrawable#LINEAR_GRADIENT, android.graphics.drawable.GradientDrawable#RADIAL_GRADIENT, or android.graphics.drawable.GradientDrawable#SWEEP_GRADIENT

getInnerRadius

Added in API level 29
open fun getInnerRadius(): Int

Return the inner radius of the ring
The units of this value are pixels.

Return
Int The units of this value are pixels. {}

getInnerRadiusRatio

Added in API level 29
open fun getInnerRadiusRatio(): Float

Return the inner radius of the ring expressed as a ratio of the ring's width.

getIntrinsicHeight

Added in API level 1
open fun getIntrinsicHeight(): Int
Return
Int the intrinsic height, or -1 if no intrinsic height

getIntrinsicWidth

Added in API level 1
open fun getIntrinsicWidth(): Int
Return
Int the intrinsic width, or -1 if no intrinsic width

getOpacity

Added in API level 1
open fun getOpacity(): Int
Return
Int int The opacity class of the Drawable. Value is android.graphics.PixelFormat#UNKNOWN, android.graphics.PixelFormat#TRANSLUCENT, android.graphics.PixelFormat#TRANSPARENT, or android.graphics.PixelFormat#OPAQUE

getOpticalInsets

Added in API level 29
open fun getOpticalInsets(): Insets
Return
Insets This value cannot be null.

getOrientation

Added in API level 16
open fun getOrientation(): GradientDrawable.Orientation!

Returns the orientation of the gradient defined in this drawable.

Return
GradientDrawable.Orientation! the orientation of the gradient defined in this drawable

getOutline

Added in API level 21
open fun getOutline(outline: Outline): Unit
Parameters
outline Outline: This value cannot be null.

getPadding

Added in API level 1
open fun getPadding(padding: Rect): Boolean
Parameters
padding Rect: This value cannot be null.
Return
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
open fun getShape(): Int

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

Return
Int the type of shape used by this drawable Value is android.graphics.drawable.GradientDrawable#RECTANGLE, android.graphics.drawable.GradientDrawable#OVAL, android.graphics.drawable.GradientDrawable#LINE, or android.graphics.drawable.GradientDrawable#RING

See Also

getThickness

Added in API level 29
open fun getThickness(): Int

Return the thickness of the ring
The units of this value are pixels.

Return
Int The units of this value are pixels. {}

getThicknessRatio

Added in API level 29
open fun getThicknessRatio(): Float

Return the thickness ratio of the ring expressed as a ratio of the ring's width.

getUseLevel

Added in API level 24
open fun getUseLevel(): Boolean

Returns whether this drawable's level property will be used to scale the gradient.

Return
Boolean true if the gradient should be scaled based on level, false otherwise

hasFocusStateSpecified

Added in API level 31
open fun hasFocusStateSpecified(): Boolean
Return
Boolean true if android.R.attr#state_focused is specified for this drawable.

inflate

Added in API level 21
open fun inflate(
    r: Resources,
    parser: XmlPullParser,
    attrs: AttributeSet,
    theme: Resources.Theme?
): Unit

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: This value cannot be null.
parser XmlPullParser: This value cannot be null.
attrs AttributeSet: This value cannot be null.
theme Resources.Theme?: This value may be null.
Exceptions
org.xmlpull.v1.XmlPullParserException
java.io.IOException

isStateful

Added in API level 1
open fun isStateful(): Boolean
Return
Boolean True if this drawable changes its appearance based on state, false otherwise.

mutate

Added in API level 3
open fun mutate(): Drawable
Return
Drawable This drawable. This value cannot be null.

setAlpha

Added in API level 1
open fun setAlpha(alpha: Int): Unit
Parameters
alpha Int: Value is between 0 and 255 inclusive

setColor

Added in API level 1
open fun setColor(argb: Int): Unit

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

setColor

Added in API level 21
open fun setColor(colorStateList: ColorStateList?): Unit

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 Color#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 This value may be null.

See Also

setColorFilter

Added in API level 1
open fun setColorFilter(colorFilter: ColorFilter?): Unit

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?: This value may be null.

setColors

Added in API level 16
open fun setColors(colors: IntArray?): Unit

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 IntArray?: an array containing 2 or more ARGB colors This value may be null.

setColors

Added in API level 29
open fun setColors(
    colors: IntArray?,
    offsets: FloatArray?
): Unit

Sets the colors and offsets 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 IntArray?: an array containing 2 or more ARGB colors This value may be null.
offsets FloatArray?: optional array of floating point parameters representing the positions of the colors. Null evenly disperses the colors This value may be null.

setCornerRadii

Added in API level 1
open fun setCornerRadii(radii: FloatArray?): Unit

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 FloatArray?: an array of length >= 8 containing 4 pairs of X and Y radius for each corner, specified in pixels This value may be null.

setCornerRadius

Added in API level 1
open fun setCornerRadius(radius: Float): Unit

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

setDither

Added in API level 1
open fun setDither(dither: Boolean): Unit

setGradientCenter

Added in API level 1
open fun setGradientCenter(
    x: Float,
    y: Float
): Unit

Sets the position of the center of the gradient as a fraction of the width and height.

The default value is (0.5, 0.5).

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-position of the center of the gradient
y Float: the Y-position of the center of the gradient

setGradientRadius

Added in API level 1
open fun setGradientRadius(gradientRadius: Float): Unit

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

setGradientType

Added in API level 1
open fun setGradientType(gradient: Int): Unit

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 Value is android.graphics.drawable.GradientDrawable#LINEAR_GRADIENT, android.graphics.drawable.GradientDrawable#RADIAL_GRADIENT, or android.graphics.drawable.GradientDrawable#SWEEP_GRADIENT

setInnerRadius

Added in API level 29
open fun setInnerRadius(innerRadius: Int): Unit

Configure the inner radius of the ring.

Parameters
innerRadius Int: The units of this value are pixels.

setInnerRadiusRatio

Added in API level 29
open fun setInnerRadiusRatio(innerRadiusRatio: Float): Unit

Inner radius of the ring expressed as a ratio of the ring's width.

Parameters
innerRadiusRatio Float: Value is 0.0f or greater

setOrientation

Added in API level 16
open fun setOrientation(orientation: GradientDrawable.Orientation!): Unit

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

setPadding

Added in API level 29
open fun setPadding(
    left: Int,
    top: Int,
    right: Int,
    bottom: Int
): Unit

Configure the padding of the gradient shape

Parameters
left Int: Left padding of the gradient shape The units of this value are pixels.
top Int: Top padding of the gradient shape The units of this value are pixels.
right Int: Right padding of the gradient shape The units of this value are pixels.
bottom Int: Bottom padding of the gradient shape The units of this value are pixels.

setShape

Added in API level 1
open fun setShape(shape: Int): Unit

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 Value is android.graphics.drawable.GradientDrawable#RECTANGLE, android.graphics.drawable.GradientDrawable#OVAL, android.graphics.drawable.GradientDrawable#LINE, or android.graphics.drawable.GradientDrawable#RING

See Also

setSize

Added in API level 1
open fun setSize(
    width: Int,
    height: Int
): Unit

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

setStroke

Added in API level 1
open fun setStroke(
    width: Int,
    color: Int
): Unit

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

setStroke

Added in API level 21
open fun setStroke(
    width: Int,
    colorStateList: ColorStateList!
): Unit

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

setStroke

Added in API level 1
open fun setStroke(
    width: Int,
    color: Int,
    dashWidth: Float,
    dashGap: Float
): Unit

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

setStroke

Added in API level 21
open fun setStroke(
    width: Int,
    colorStateList: ColorStateList!,
    dashWidth: Float,
    dashGap: Float
): Unit

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

setThickness

Added in API level 29
open fun setThickness(thickness: Int): Unit

Configure the thickness of the ring.

Parameters
thickness Int: The units of this value are pixels.

setThicknessRatio

Added in API level 29
open fun setThicknessRatio(thicknessRatio: Float): Unit

Configure the thickness of the ring expressed as a ratio of the ring's width.

Parameters
thicknessRatio Float: Value is 0.0f or greater

setTintBlendMode

Added in API level 29
open fun setTintBlendMode(blendMode: BlendMode!): Unit

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 BlendMode#SRC_IN.

Note: Setting a color filter via setColorFilter(android.graphics.ColorFilter)

Parameters
blendMode BlendMode!: This value cannot be null.

setTintList

Added in API level 21
open fun setTintList(tint: ColorStateList?): Unit

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,android.graphics.PorterDuff.Mode).

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

Parameters
tint ColorStateList?: This value may be null.

setUseLevel

Added in API level 1
open fun setUseLevel(useLevel: Boolean): Unit

Sets whether this drawable's level property will be used to scale the gradient. If a gradient is not used, this property has no effect.

Scaling behavior varies based on gradient type:

  • LINEAR_GRADIENT adjusts the ending position along the gradient's axis of orientation (see getOrientation())
  • RADIAL_GRADIENT adjusts the outer radius
  • SWEEP_GRADIENT adjusts the ending angle

      The default value for this property is false.

      Note: This property corresponds to the android:useLevel attribute on the inner <gradient> tag, NOT the android:useLevel attribute on the outer <shape> tag. For example,

      <code>&lt;shape ...&gt;
            &lt;gradient
                ...
                android:useLevel="true" /&gt;
        &lt;/shape&gt;
        </code>

      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 the gradient should be scaled based on level, false otherwise

Protected methods

onBoundsChange

Added in API level 1
protected open fun onBoundsChange(r: Rect): Unit
Parameters
bounds This value cannot be null.

onLevelChange

Added in API level 1
protected open fun onLevelChange(level: Int): Boolean
Return
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
protected open fun onStateChange(stateSet: IntArray): Boolean
Parameters
state This value cannot be null.
Return
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.