Added in API level 1

InsetDrawable


open class InsetDrawable : DrawableWrapper
kotlin.Any
   ↳ android.graphics.drawable.Drawable
   ↳ android.graphics.drawable.DrawableWrapper
   ↳ android.graphics.drawable.InsetDrawable

A Drawable that insets another Drawable by a specified distance or fraction of the content bounds. This is used when a View needs a background that is smaller than the View's actual bounds.

It can be defined in an XML file with the <inset> element. For more information, see the guide to Drawable Resources.

The inset values are added to the padding of the view. For instance, if a view has a padding of 10 pixels on all sides and an InsetDrawable with an inset of 5 pixels is set as its background, the effective padding of the view will be 15 pixels on all sides.

Note: When using fractional insets (floats), the intrinsic size calculation is influenced by insets on opposite sides. For example, applying a fractional inset to only the bottom of a drawable may also affect the drawable's top padding. For insets that should only affect a single side, using dimension-based insets (integers) will provide that behavior.

Summary

XML attributes
android:drawable Reference to a drawable resource to use for the frame.
android:insetBottom
android:insetLeft
android:insetRight
android:insetTop
android:visible Provides initial visibility state of the drawable; the default value is false.
Public constructors
InsetDrawable(drawable: Drawable?, inset: Float)

Creates a new inset drawable with the specified inset.

InsetDrawable(drawable: Drawable?, insetLeftFraction: Float, insetTopFraction: Float, insetRightFraction: Float, insetBottomFraction: Float)

Creates a new inset drawable with the specified insets in fraction of the view bounds.

InsetDrawable(drawable: Drawable?, inset: Int)

Creates a new inset drawable with the specified inset.

InsetDrawable(drawable: Drawable?, insetLeft: Int, insetTop: Int, insetRight: Int, insetBottom: Int)

Creates a new inset drawable with the specified insets in pixels.

Public methods
open Unit

Applies the specified theme to this Drawable and its children.

open Int

Returns the drawable's intrinsic height.

open Int

Returns the drawable's intrinsic width.

open Int

Return the opacity/transparency of this Drawable.

open Insets

Return in insets the layout insets suggested by this Drawable for use with alignment operations during layout.

open Unit
getOutline(outline: Outline)

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

open Boolean
getPadding(padding: Rect)

Returns in padding the insets for this drawable.

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

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

Protected methods
open Unit

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

Inherited functions

XML attributes

android:drawable

android:drawable
Reference to a drawable resource to use for the frame. If not given, the drawable must be defined by the first child tag.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:insetBottom

android:insetBottom

android:insetLeft

android:insetLeft

android:insetRight

android:insetRight

android:insetTop

android:insetTop

android:visible

android:visible
Provides initial visibility state of the drawable; the default value is false. See android.graphics.drawable.Drawable#setVisible.

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

Public constructors

InsetDrawable

Added in API level 26
InsetDrawable(
    drawable: Drawable?,
    inset: Float)

Creates a new inset drawable with the specified inset.

Parameters
drawable Drawable?: The drawable to inset.
This value may be null.
inset Float: Inset in fraction (range: [0, 1)) of the inset content bounds.

InsetDrawable

Added in API level 26
InsetDrawable(
    drawable: Drawable?,
    insetLeftFraction: Float,
    insetTopFraction: Float,
    insetRightFraction: Float,
    insetBottomFraction: Float)

Creates a new inset drawable with the specified insets in fraction of the view bounds.

Parameters
drawable Drawable?: The drawable to inset.
This value may be null.
insetLeftFraction Float: Left inset in fraction (range: [0, 1)) of the inset content bounds.
insetTopFraction Float: Top inset in fraction (range: [0, 1)) of the inset content bounds.
insetRightFraction Float: Right inset in fraction (range: [0, 1)) of the inset content bounds.
insetBottomFraction Float: Bottom inset in fraction (range: [0, 1)) of the inset content bounds.

InsetDrawable

Added in API level 1
InsetDrawable(
    drawable: Drawable?,
    inset: Int)

Creates a new inset drawable with the specified inset.

Parameters
drawable Drawable?: The drawable to inset.
This value may be null.
inset Int: Inset in pixels around the drawable.

InsetDrawable

Added in API level 1
InsetDrawable(
    drawable: Drawable?,
    insetLeft: Int,
    insetTop: Int,
    insetRight: Int,
    insetBottom: Int)

Creates a new inset drawable with the specified insets in pixels.

Parameters
drawable Drawable?: The drawable to inset.
This value may be null.
insetLeft Int: Left inset in pixels.
insetTop Int: Top inset in pixels.
insetRight Int: Right inset in pixels.
insetBottom Int: Bottom inset in pixels.

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.

getIntrinsicHeight

Added in API level 1
open fun getIntrinsicHeight(): Int

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.

The intrinsic height is calculated based on the combination of both top and bottom fractional insets. For example, applying a fractional inset to only the bottom of a drawable will also influence the final size calculation for the top.

Return
Int the intrinsic height, or -1 if no intrinsic height

getIntrinsicWidth

Added in API level 1
open fun getIntrinsicWidth(): Int

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.

The intrinsic width is calculated based on the combination of both left and right fractional insets. This means a fractional inset on one side will influence the final size calculation for both sides.

Return
Int the intrinsic width, or -1 if no intrinsic width

getOpacity

Added in API level 1
open fun getOpacity(): Int

Return the opacity/transparency of this Drawable. The returned value is one of the abstract format constants in android.graphics.PixelFormat: android.graphics.PixelFormat#UNKNOWN, android.graphics.PixelFormat#TRANSLUCENT, android.graphics.PixelFormat#TRANSPARENT, or android.graphics.PixelFormat#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 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 or #setColorFilter methods. Some subclasses, such as BitmapDrawable, ColorDrawable, and GradientDrawable, do account for the value of setAlpha, but the general behavior is dependent upon the implementation of the subclass.

Return
Int int The opacity class of the Drawable.
Value is one of the following:

getOpticalInsets

Added in API level 29
open fun getOpticalInsets(): Insets

Return in insets the layout insets suggested by this Drawable for use with alignment operations during layout.

Return
Insets This value cannot be null.

getOutline

Added in API level 21
open fun getOutline(outline: Outline): Unit

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

This method is called by the default android.view.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: This value cannot be null.

getPadding

Added in API level 1
open fun getPadding(padding: Rect): Boolean

Returns in padding the insets for this drawable.

The returned padding is the sum of the wrapped drawable's padding and the insets of this InsetDrawable. This means that if the inset value is 0, the resulting padding will be the same as the wrapped drawable's padding.

Parameters
padding Rect: The Rect to receive the padding.
Return
Boolean True if this drawable has a non-zero padding or non-zero insets, else false.

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
java.io.IOException
org.xmlpull.v1.XmlPullParserException

Protected methods

onBoundsChange

Added in API level 1
protected open fun onBoundsChange(bounds: Rect): Unit

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

Parameters
bounds Rect: This value cannot be null.