Added in API level 1

GradientDrawable


open class GradientDrawable : Drawable

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

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

open Unit
setColor(argb: Int)

Changes this drawable to use a single color 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, colorStateList: ColorStateList!)

Set the stroke width and color state list 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
setStroke(width: Int, color: Int)

Set the stroke width and color 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
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
Unit clearColorFilter()

Removes the color filter for this drawable.

Rect copyBounds()

Return a copy of the drawable's bounds in a new Rect. This returns the same values as getBounds(), but the returned object is guaranteed to not be changed later by the drawable (i.e. it retains no reference to this rect). If the caller already has a Rect allocated, call copyBounds(rect).

Unit copyBounds(bounds: Rect)

Return a copy of the drawable's bounds in the specified Rect (allocated by the caller). The bounds specify where this will draw when its draw() method is called.

Drawable? createFromPath(pathName: String!)

Create a drawable from file path name.

Drawable? createFromResourceStream(res: Resources?, value: TypedValue?, is: InputStream?, srcName: String?)

Create a drawable from an inputstream, using the given resources and value to determine density information.

Drawable? createFromResourceStream(res: Resources?, value: TypedValue?, is: InputStream?, srcName: String?, opts: BitmapFactory.Options?)

Create a drawable from an inputstream, using the given resources and value to determine density information.

Drawable? createFromStream(is: InputStream?, srcName: String?)

Create a drawable from an inputstream

Drawable createFromXml(r: Resources, parser: XmlPullParser)

Create a drawable from an XML document. For more information on how to create resources in XML, see Drawable Resources.

Drawable createFromXml(r: Resources, parser: XmlPullParser, theme: Resources.Theme?)

Create a drawable from an XML document using an optional Theme. For more information on how to create resources in XML, see Drawable Resources.

Drawable createFromXmlInner(r: Resources, parser: XmlPullParser, attrs: AttributeSet)

Create from inside an XML document. Called on a parser positioned at a tag in an XML document, tries to create a Drawable from that tag. Returns null if the tag is not a valid drawable.

Drawable createFromXmlInner(r: Resources, parser: XmlPullParser, attrs: AttributeSet, theme: Resources.Theme?)

Create a drawable from inside an XML document using an optional Theme. Called on a parser positioned at a tag in an XML document, tries to create a Drawable from that tag. Returns null if the tag is not a valid drawable.

Rect getBounds()

Return the drawable's bounds Rect. Note: for efficiency, the returned object may be the same object stored in the drawable (though this is not guaranteed), so if a persistent copy of the bounds is needed, call copyBounds(rect) instead. You should also not change the object returned by this method as it may be the same object stored in the drawable.

Drawable.Callback? getCallback()

Return the current Callback implementation attached to this Drawable.

Drawable getCurrent()

Rect getDirtyBounds()

Return the drawable's dirty bounds Rect. Note: for efficiency, the returned object may be the same object stored in the drawable (though this is not guaranteed).

By default, this returns the full drawable bounds. Custom drawables may override this method to perform more precise invalidation.

Unit getHotspotBounds(outRect: Rect)

Populates outRect with the hotspot bounds.

Int getLayoutDirection()

Returns the resolved layout direction for this Drawable.

Int getLevel()

Retrieve the current level.

Int getMinimumHeight()

Returns the minimum height suggested by this Drawable. If a View uses this Drawable as a background, it is suggested that the View use at least this value for its height. (There will be some scenarios where this will not be possible.) This value should INCLUDE any padding.

Int getMinimumWidth()

Returns the minimum width suggested by this Drawable. If a View uses this Drawable as a background, it is suggested that the View use at least this value for its width. (There will be some scenarios where this will not be possible.) This value should INCLUDE any padding.

IntArray getState()

Describes the current state, as a union of primitive states, such as android.R.attr#state_focused, android.R.attr#state_selected, etc. Some drawables may modify their imagery based on the selected state.

Region? getTransparentRegion()

Returns a Region representing the part of the Drawable that is completely transparent. This can be used to perform drawing operations, identifying which parts of the target will not change when rendering the Drawable. The default implementation returns null, indicating no transparent region; subclasses can optionally override this to return an actual Region if they want to supply this optimization information, but it is not required that they do so.

Unit inflate(r: Resources, parser: XmlPullParser, attrs: AttributeSet)

Inflate this Drawable from an XML resource. Does not apply a theme.

Unit invalidateSelf()

Use the current Callback implementation to have this Drawable redrawn. Does nothing if there is no Callback attached to the Drawable.

Boolean isAutoMirrored()

Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left. See android.util.LayoutDirection.

Boolean isFilterBitmap()

Boolean isProjected()

Whether this drawable requests projection. Indicates that the android.graphics.RenderNode this Drawable will draw into should be drawn immediately after the closest ancestor RenderNode containing a projection receiver.

Boolean isVisible()

Unit jumpToCurrentState()

If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.

Boolean onLayoutDirectionChanged(layoutDirection: Int)

Called when the drawable's resolved layout direction changes.

Int resolveOpacity(op1: Int, op2: Int)

Return the appropriate opacity value for two source opacities. If either is UNKNOWN, that is returned; else, if either is TRANSLUCENT, that is returned; else, if either is TRANSPARENT, that is returned; else, OPAQUE is returned.

This is to help in implementing getOpacity.

Unit scheduleSelf(what: Runnable, when: Long)

Use the current Callback implementation to have this Drawable scheduled. Does nothing if there is no Callback attached to the Drawable.

Unit setAutoMirrored(mirrored: Boolean)

Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left). See android.util.LayoutDirection.

Unit setBounds(bounds: Rect)

Specify a bounding rectangle for the Drawable. This is where the drawable will draw when its draw() method is called.

Unit setBounds(left: Int, top: Int, right: Int, bottom: Int)

Specify a bounding rectangle for the Drawable. This is where the drawable will draw when its draw() method is called.

Unit setCallback(cb: Drawable.Callback?)

Bind a Callback object to this Drawable. Required for clients that want to support animated drawables.

Unit setChangingConfigurations(configs: Int)

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

Unit setColorFilter(color: Int, mode: PorterDuff.Mode)

Specify a color and Porter-Duff mode to be the color filter for this drawable.

Convenience for setColorFilter(android.graphics.ColorFilter) which constructs a PorterDuffColorFilter.

Note: Setting a color filter disables tint.

Unit setFilterBitmap(filter: Boolean)

Set to true to have the drawable filter its bitmaps with bilinear sampling when they are scaled or rotated.

This can improve appearance when bitmaps are rotated. If the drawable does not use bitmaps, this call is ignored.

Unit setHotspot(x: Float, y: Float)

Specifies the hotspot's location within the drawable.

Unit setHotspotBounds(left: Int, top: Int, right: Int, bottom: Int)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

Boolean setLayoutDirection(layoutDirection: Int)

Set the layout direction for this drawable. Should be a resolved layout direction, as the Drawable has no capacity to do the resolution on its own.

Boolean setLevel(level: Int)

Specify the level for the drawable. This allows a drawable to vary its imagery based on a continuous controller, for example to show progress or volume level.

If the new level you are supplying causes the appearance of the Drawable to change, then it is responsible for calling invalidateSelf in order to have itself redrawn, and true will be returned from this function.

Boolean setState(stateSet: IntArray)

Specify a set of states for the drawable. These are use-case specific, so see the relevant documentation. As an example, the background for widgets like Button understand the following states: [android.R.attr#state_focused, android.R.attr#state_pressed].

If the new state you are supplying causes the appearance of the Drawable to change, then it is responsible for calling invalidateSelf in order to have itself redrawn, and true will be returned from this function.

Note: The Drawable holds a reference on to stateSet until a new state array is given to it, so you must not modify this array during that time.

Unit setTint(tintColor: Int)

Specifies tint color for this drawable.

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

To clear the tint, pass null to setTintList(android.content.res.ColorStateList).

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

Unit setTintMode(tintMode: PorterDuff.Mode?)

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 PorterDuff.Mode.SRC_IN.

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

Boolean setVisible(visible: Boolean, restart: Boolean)

Set whether this Drawable is visible. This generally does not impact the Drawable's behavior, but is a hint that can be used by some Drawables, for example, to decide whether run animations.

Unit unscheduleSelf(what: Runnable)

Use the current Callback implementation to have this Drawable unscheduled. Does nothing if there is no Callback attached to the Drawable.

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.

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

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.

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

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

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.

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.

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

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

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.