# DrawableMarginSpan

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#pubctors)\|[Methods](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#inhmethods)  

# DrawableMarginSpan

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/text/style/DrawableMarginSpan "View this page in Kotlin")\|Java

`
public


class
DrawableMarginSpan
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


implements

`[LeadingMarginSpan](https://developer.android.com/reference/android/text/style/LeadingMarginSpan)`,

`[LineHeightSpan](https://developer.android.com/reference/android/text/style/LineHeightSpan)`


`

|---|---------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.text.style.DrawableMarginSpan |

<br />

*** ** * ** ***

A span which adds a drawable and a padding to the paragraph it's attached to.

If the height of the drawable is bigger than the height of the line it's attached to then the line height is increased to fit the drawable.`DrawableMarginSpan`allows setting a padding between the drawable and the text. The default value is 0. The span must be set from the beginning of the text, otherwise either the span won't be rendered or it will be rendered incorrectly.

For example, a drawable and a padding of 20px can be added like this:  

    SpannableString string = new SpannableString("Text with a drawable.");
     string.setSpan(new DrawableMarginSpan(drawable, 20), 0, string.length(),
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

![](https://developer.android.com/static/reference/android/images/text/style/drawablemarginspan.png)Text with a drawable and a padding.

<br />

**See also:**

- [for working with a {@link android.graphics.Bitmap} instead of
  a {@link android.graphics.drawable.Drawable Drawable}.](https://developer.android.com/reference/android/text/style/IconMarginSpan)

## Summary

|                                                                                                                                                                                                                                                 ### Public constructors                                                                                                                                                                                                                                                  ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#DrawableMarginSpan(android.graphics.drawable.Drawable))`(`[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable)` drawable) ` Creates a[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan)from a[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable).                                         |
| ` `[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#DrawableMarginSpan(android.graphics.drawable.Drawable,%20int))`(`[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable)` drawable, int pad) ` Creates a[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan)from a[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable)and a padding, in pixels. |

|                                                                                                                                                                                                                                                                                                                                                                                                             ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                             ||
|-------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                   | ` `[chooseHeight](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#chooseHeight(java.lang.CharSequence,%20int,%20int,%20int,%20int,%20android.graphics.Paint.FontMetricsInt))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` text, int start, int end, int istartv, int v, `[Paint.FontMetricsInt](https://developer.android.com/reference/android/graphics/Paint.FontMetricsInt)` fm) ` Classes that implement this should define how the height is being calculated.                                                                                                                                                                                                          |
| ` void`                                                                                   | ` `[drawLeadingMargin](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#drawLeadingMargin(android.graphics.Canvas,%20android.graphics.Paint,%20int,%20int,%20int,%20int,%20int,%20java.lang.CharSequence,%20int,%20int,%20boolean,%20android.text.Layout))`(`[Canvas](https://developer.android.com/reference/android/graphics/Canvas)` c, `[Paint](https://developer.android.com/reference/android/graphics/Paint)` p, int x, int dir, int top, int baseline, int bottom, `[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` text, int start, int end, boolean first, `[Layout](https://developer.android.com/reference/android/text/Layout)` layout) ` Renders the leading margin. |
| ` `[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable) | ` `[getDrawable](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#getDrawable())`() ` Returns the drawable used.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ` int`                                                                                    | ` `[getLeadingMargin](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#getLeadingMargin(boolean))`(boolean first) ` Returns the amount by which to adjust the leading margin.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ` int`                                                                                    | ` `[getPadding](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#getPadding())`() ` Returns a distance between the drawable and text in pixel.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ` `[String](https://developer.android.com/reference/java/lang/String)                     | ` `[toString](https://developer.android.com/reference/android/text/style/DrawableMarginSpan#toString())`() ` Returns a string representation of the object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[android.text.style.LeadingMarginSpan](https://developer.android.com/reference/android/text/style/LeadingMarginSpan)` ` |------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[drawLeadingMargin](https://developer.android.com/reference/android/text/style/LeadingMarginSpan#drawLeadingMargin(android.graphics.Canvas,%20android.graphics.Paint,%20int,%20int,%20int,%20int,%20int,%20java.lang.CharSequence,%20int,%20int,%20boolean,%20android.text.Layout))`(`[Canvas](https://developer.android.com/reference/android/graphics/Canvas)` c, `[Paint](https://developer.android.com/reference/android/graphics/Paint)` p, int x, int dir, int top, int baseline, int bottom, `[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` text, int start, int end, boolean first, `[Layout](https://developer.android.com/reference/android/text/Layout)` layout) ` Renders the leading margin. | | ` abstract int`  | ` `[getLeadingMargin](https://developer.android.com/reference/android/text/style/LeadingMarginSpan#getLeadingMargin(boolean))`(boolean first) ` Returns the amount by which to adjust the leading margin.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | ||
| From interface` `[android.text.style.LineHeightSpan](https://developer.android.com/reference/android/text/style/LineHeightSpan)` ` |------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[chooseHeight](https://developer.android.com/reference/android/text/style/LineHeightSpan#chooseHeight(java.lang.CharSequence,%20int,%20int,%20int,%20int,%20android.graphics.Paint.FontMetricsInt))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` text, int start, int end, int spanstartv, int lineHeight, `[Paint.FontMetricsInt](https://developer.android.com/reference/android/graphics/Paint.FontMetricsInt)` fm) ` Classes that implement this should define how the height is being calculated. | ||

## Public constructors

### DrawableMarginSpan

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DrawableMarginSpan (Drawable drawable)
```

Creates a[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan)from a[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable). The pad width will be 0.

<br />

|                                     Parameters                                      ||
|------------|-------------------------------------------------------------------------|
| `drawable` | `Drawable`: the drawable to be added This value cannot be`null`. <br /> |

### DrawableMarginSpan

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DrawableMarginSpan (Drawable drawable, 
                int pad)
```

Creates a[DrawableMarginSpan](https://developer.android.com/reference/android/text/style/DrawableMarginSpan)from a[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable)and a padding, in pixels.

<br />

|                                     Parameters                                      ||
|------------|-------------------------------------------------------------------------|
| `drawable` | `Drawable`: the drawable to be added This value cannot be`null`. <br /> |
| `pad`      | `int`: the distance between the drawable and the text <br />            |

## Public methods

### chooseHeight

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void chooseHeight (CharSequence text, 
                int start, 
                int end, 
                int istartv, 
                int v, 
                Paint.FontMetricsInt fm)
```

Classes that implement this should define how the height is being calculated.

<br />

|                              Parameters                               ||
|-----------|------------------------------------------------------------|
| `text`    | `CharSequence`: This value cannot be`null`. <br />         |
| `start`   | `int`: the start of the line <br />                        |
| `end`     | `int`: the end of the line <br />                          |
| `istartv` | `int`: the start of the span <br />                        |
| `v`       | `int`: the line height <br />                              |
| `fm`      | `Paint.FontMetricsInt`: This value cannot be`null`. <br /> |

### drawLeadingMargin

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void drawLeadingMargin (Canvas c, 
                Paint p, 
                int x, 
                int dir, 
                int top, 
                int baseline, 
                int bottom, 
                CharSequence text, 
                int start, 
                int end, 
                boolean first, 
                Layout layout)
```

Renders the leading margin. This is called before the margin has been adjusted by the value returned by[getLeadingMargin(boolean)](https://developer.android.com/reference/android/text/style/LeadingMarginSpan#getLeadingMargin(boolean)).

<br />

|                                                                    Parameters                                                                    ||
|------------|--------------------------------------------------------------------------------------------------------------------------------------|
| `c`        | `Canvas`: This value cannot be`null`. <br />                                                                                         |
| `p`        | `Paint`: This value cannot be`null`. <br />                                                                                          |
| `x`        | `int`: the current position of the margin <br />                                                                                     |
| `dir`      | `int`: the base direction of the paragraph; if negative, the margin is to the right of the text, otherwise it is to the left. <br /> |
| `top`      | `int`: the top of the line <br />                                                                                                    |
| `baseline` | `int`: the baseline of the line <br />                                                                                               |
| `bottom`   | `int`: the bottom of the line <br />                                                                                                 |
| `text`     | `CharSequence`: This value cannot be`null`. <br />                                                                                   |
| `start`    | `int`: the start of the line <br />                                                                                                  |
| `end`      | `int`: the end of the line <br />                                                                                                    |
| `first`    | `boolean`: true if this is the first line of its paragraph <br />                                                                    |
| `layout`   | `Layout`: This value cannot be`null`. <br />                                                                                         |

### getDrawable

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Drawable getDrawable ()
```

Returns the drawable used.

<br />

|                                                                Returns                                                                ||
|----------------------------------------------------------------------------------------|-----------------------------------------------|
| [Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable) | a drawable This value cannot be`null`. <br /> |

### getLeadingMargin

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getLeadingMargin (boolean first)
```

Returns the amount by which to adjust the leading margin. Positive values move away from the leading edge of the paragraph, negative values move towards it.

<br />

|                                                   Parameters                                                    ||
|---------|--------------------------------------------------------------------------------------------------------|
| `first` | `boolean`: true if the request is for the first line of a paragraph, false for subsequent lines <br /> |

|                 Returns                  ||
|-------|-----------------------------------|
| `int` | the offset for the margin. <br /> |

### getPadding

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getPadding ()
```

Returns a distance between the drawable and text in pixel.  
The units of this value are pixels.

<br />

|                                     Returns                                      ||
|-------|---------------------------------------------------------------------------|
| `int` | a distance pixel from the text The units of this value are pixels. <br /> |

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns a string representation of the object.

<br />

|                                                      Returns                                                      ||
|--------------------------------------------------------------------|-----------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the object. <br /> |