Skip to content

Most visited

Recently visited

navigation

ImageSpan

public class ImageSpan
extends DynamicDrawableSpan

java.lang.Object
   ↳ android.text.style.CharacterStyle
     ↳ android.text.style.MetricAffectingSpan
       ↳ android.text.style.ReplacementSpan
         ↳ android.text.style.DynamicDrawableSpan
           ↳ android.text.style.ImageSpan


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. The drawable can be constructed from varied sources:

The default value for the vertical alignment is ALIGN_BOTTOM

For example, an ImagedSpan can be used like this:

 SpannableString string = SpannableString("Bottom: span.\nBaseline: span.");
 // using the default alignment: ALIGN_BOTTOM
 string.setSpan(ImageSpan(this, R.mipmap.ic_launcher), 7, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 string.setSpan(ImageSpan(this, R.mipmap.ic_launcher, DynamicDrawableSpan.ALIGN_BASELINE),
 22, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
Text with ImageSpans aligned bottom and baseline.

Summary

Inherited constants

Inherited fields

Public constructors

ImageSpan(Bitmap b)

This constructor was deprecated in API level 4. Use ImageSpan(Context, Bitmap) instead.

ImageSpan(Bitmap b, int verticalAlignment)

This constructor was deprecated in API level 4. Use ImageSpan(Context, Bitmap, int) instead.

ImageSpan(Context context, Bitmap bitmap)

Constructs an ImageSpan from a Context and a Bitmap with the default alignment ALIGN_BOTTOM

ImageSpan(Context context, Bitmap bitmap, int verticalAlignment)

Constructs an ImageSpan from a Context, a Bitmap and a vertical alignment.

ImageSpan(Drawable drawable)

Constructs an ImageSpan from a drawable with the default alignment ALIGN_BOTTOM.

ImageSpan(Drawable drawable, int verticalAlignment)

Constructs an ImageSpan from a drawable and a vertical alignment.

ImageSpan(Drawable drawable, String source)

Constructs an ImageSpan from a drawable and a source with the default alignment ALIGN_BOTTOM

ImageSpan(Drawable drawable, String source, int verticalAlignment)

Constructs an ImageSpan from a drawable, a source and a vertical alignment.

ImageSpan(Context context, Uri uri)

Constructs an ImageSpan from a Context and a Uri with the default alignment ALIGN_BOTTOM.

ImageSpan(Context context, Uri uri, int verticalAlignment)

Constructs an ImageSpan from a Context, a Uri and a vertical alignment.

ImageSpan(Context context, int resourceId)

Constructs an ImageSpan from a Context and a resource id with the default alignment ALIGN_BOTTOM

ImageSpan(Context context, int resourceId, int verticalAlignment)

Constructs an ImageSpan from a Context, a resource id and a vertical alignment.

Public methods

Drawable getDrawable()

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

String getSource()

Returns the source string that was saved during construction.

Inherited methods

Public constructors

ImageSpan

added in API level 1
ImageSpan (Bitmap b)

This constructor was deprecated in API level 4.
Use ImageSpan(Context, Bitmap) instead.

Parameters
b Bitmap

This value must never be null.

ImageSpan

added in API level 3
ImageSpan (Bitmap b, 
                int verticalAlignment)

This constructor was deprecated in API level 4.
Use ImageSpan(Context, Bitmap, int) instead.

Parameters
b Bitmap

This value must never be null.

verticalAlignment int

ImageSpan

added in API level 4
ImageSpan (Context context, 
                Bitmap bitmap)

Constructs an ImageSpan from a Context and a Bitmap with the default alignment ALIGN_BOTTOM

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources

This value must never be null.

bitmap Bitmap: bitmap to be rendered

This value must never be null.

ImageSpan

added in API level 4
ImageSpan (Context context, 
                Bitmap bitmap, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a Bitmap and a vertical alignment.

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources

This value must never be null.

bitmap Bitmap: bitmap to be rendered

This value must never be null.

verticalAlignment int: one of ALIGN_BOTTOM or ALIGN_BASELINE

ImageSpan

added in API level 1
ImageSpan (Drawable drawable)

Constructs an ImageSpan from a drawable with the default alignment ALIGN_BOTTOM.

Parameters
drawable Drawable: drawable to be rendered

This value must never be null.

ImageSpan

added in API level 3
ImageSpan (Drawable drawable, 
                int verticalAlignment)

Constructs an ImageSpan from a drawable and a vertical alignment.

Parameters
drawable Drawable: drawable to be rendered

This value must never be null.

verticalAlignment int: one of ALIGN_BOTTOM or ALIGN_BASELINE

ImageSpan

added in API level 1
ImageSpan (Drawable drawable, 
                String source)

Constructs an ImageSpan from a drawable and a source with the default alignment ALIGN_BOTTOM

Parameters
drawable Drawable: drawable to be rendered

This value must never be null.

source String: drawable's Uri source

This value must never be null.

ImageSpan

added in API level 3
ImageSpan (Drawable drawable, 
                String source, 
                int verticalAlignment)

Constructs an ImageSpan from a drawable, a source and a vertical alignment.

Parameters
drawable Drawable: drawable to be rendered

This value must never be null.

source String: drawable's uri source

This value must never be null.

verticalAlignment int: one of ALIGN_BOTTOM or ALIGN_BASELINE

ImageSpan

added in API level 1
ImageSpan (Context context, 
                Uri uri)

Constructs an ImageSpan from a Context and a Uri with the default alignment ALIGN_BOTTOM. The Uri source can be retrieved via getSource()

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources

This value must never be null.

uri Uri: Uri used to construct the drawable that will be rendered

This value must never be null.

ImageSpan

added in API level 3
ImageSpan (Context context, 
                Uri uri, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a Uri and a vertical alignment. The Uri source can be retrieved via getSource()

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources

This value must never be null.

uri Uri: Uri used to construct the drawable that will be rendered.

This value must never be null.

verticalAlignment int: one of ALIGN_BOTTOM or ALIGN_BASELINE

ImageSpan

added in API level 1
ImageSpan (Context context, 
                int resourceId)

Constructs an ImageSpan from a Context and a resource id with the default alignment ALIGN_BOTTOM

Parameters
context Context: context used to retrieve the drawable from resources

This value must never be null.

resourceId int: drawable resource id based on which the drawable is retrieved

ImageSpan

added in API level 3
ImageSpan (Context context, 
                int resourceId, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a resource id and a vertical alignment.

Parameters
context Context: context used to retrieve the drawable from resources

This value must never be null.

resourceId int: drawable resource id based on which the drawable is retrieved.

verticalAlignment int: one of ALIGN_BOTTOM or ALIGN_BASELINE

Public methods

getDrawable

added in API level 1
Drawable getDrawable ()

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.

Returns
Drawable

getSource

added in API level 1
String getSource ()

Returns the source string that was saved during construction.

Returns
String the source string that was saved during construction

This value may be null.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)