Added in API level 1

DynamicDrawableSpan


abstract class DynamicDrawableSpan : ReplacementSpan
kotlin.Any
   ↳ android.text.style.CharacterStyle
   ↳ android.text.style.MetricAffectingSpan
   ↳ android.text.style.ReplacementSpan
   ↳ android.text.style.DynamicDrawableSpan

Span that replaces the text it's attached to with a Drawable that can be aligned with the bottom or with the baseline of the surrounding text.

For an implementation that constructs the drawable from various sources (Bitmap, Drawable, resource id or Uri) use ImageSpan.

A simple implementation of DynamicDrawableSpan that uses drawables from resources looks like this:

class MyDynamicDrawableSpan extends DynamicDrawableSpan {
 
  private final Context mContext;
  private final int mResourceId;
 
  public MyDynamicDrawableSpan(Context context, @DrawableRes int resourceId) {
      mContext = context;
      mResourceId = resourceId;
  }
 
  @Override
  public Drawable getDrawable() {
       Drawable drawable = mContext.getDrawable(mResourceId);
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
       return drawable;
  }
  }
The class can be used like this:
SpannableString string = new SpannableString("Text with a drawable span");
  string.setSpan(new MyDynamicDrawableSpan(context, R.mipmap.ic_launcher), 12, 20, Spanned
  .SPAN_EXCLUSIVE_EXCLUSIVE);
Replacing text with a drawable.

Summary

Constants
static Int

A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.

static Int

A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, i.

static Int

A constant indicating that this span should be vertically centered between the top and the lowest descender.

Public constructors

Creates a DynamicDrawableSpan.

Protected constructors
DynamicDrawableSpan(verticalAlignment: Int)

Creates a DynamicDrawableSpan based on a vertical alignment.

Public methods
open Unit
draw(canvas: Canvas, text: CharSequence!, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint)

Draws the span into the canvas.

abstract Drawable!

Your subclass must implement this method to provide the bitmap to be drawn.

open Int
getSize(paint: Paint, text: CharSequence!, start: Int, end: Int, fm: Paint.FontMetricsInt?)

Returns the width of the span.

open Int

Returns the vertical alignment of this span, one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER.

open String

Inherited functions
Properties
Int

Constants

ALIGN_BASELINE

Added in API level 3
static val ALIGN_BASELINE: Int

A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.

Value: 1

ALIGN_BOTTOM

Added in API level 3
static val ALIGN_BOTTOM: Int

A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, i.e., at the same level as the lowest descender in the text.

Value: 0

ALIGN_CENTER

Added in API level 29
static val ALIGN_CENTER: Int

A constant indicating that this span should be vertically centered between the top and the lowest descender.

Value: 2

Public constructors

DynamicDrawableSpan

Added in API level 1
DynamicDrawableSpan()

Creates a DynamicDrawableSpan. The default vertical alignment is ALIGN_BOTTOM

Protected constructors

DynamicDrawableSpan

Added in API level 3
protected DynamicDrawableSpan(verticalAlignment: Int)

Creates a DynamicDrawableSpan based on a vertical alignment.\

Parameters
verticalAlignment Int: one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER Value is android.text.style.DynamicDrawableSpan#ALIGN_BOTTOM, android.text.style.DynamicDrawableSpan#ALIGN_BASELINE, or android.text.style.DynamicDrawableSpan#ALIGN_CENTER

Public methods

draw

Added in API level 1
open fun draw(
    canvas: Canvas,
    text: CharSequence!,
    start: Int,
    end: Int,
    x: Float,
    top: Int,
    y: Int,
    bottom: Int,
    paint: Paint
): Unit

Draws the span into the canvas.

Parameters
canvas Canvas: This value cannot be null.
text CharSequence!: Current text.
start Int: Value is 0 or greater
end Int: Value is 0 or greater
x Float: Edge of the replacement closest to the leading margin.
top Int: Top of the line.
y Int: Baseline.
bottom Int: Bottom of the line.
paint Paint: This value cannot be null.

getDrawable

Added in API level 1
abstract fun getDrawable(): Drawable!

Your subclass must implement this method to provide the bitmap to be drawn. The dimensions of the bitmap must be the same from each call to the next.

getSize

Added in API level 1
open fun getSize(
    paint: Paint,
    text: CharSequence!,
    start: Int,
    end: Int,
    fm: Paint.FontMetricsInt?
): Int

Returns the width of the span. Extending classes can set the height of the span by updating attributes of android.graphics.Paint.FontMetricsInt. If the span covers the whole text, and the height is not set, draw(android.graphics.Canvas,java.lang.CharSequence,int,int,float,int,int,int,android.graphics.Paint) will not be called for the span.

Parameters
paint Paint: This value cannot be null.
text CharSequence!: Current text.
start Int: Value is 0 or greater
end Int: Value is 0 or greater
fm Paint.FontMetricsInt?: This value may be null.
Return
Int Width of the span.

getVerticalAlignment

Added in API level 3
open fun getVerticalAlignment(): Int

Returns the vertical alignment of this span, one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER.

Return
Int Value is android.text.style.DynamicDrawableSpan#ALIGN_BOTTOM, android.text.style.DynamicDrawableSpan#ALIGN_BASELINE, or android.text.style.DynamicDrawableSpan#ALIGN_CENTER

toString

Added in API level 1
open fun toString(): String
Return
String a string representation of the object.

Properties

mVerticalAlignment

Added in API level 3
protected val mVerticalAlignment: Int