In Android, only ViewGroup
class can contain another view, therefore it's much proper to say,
Is that possible to add a spannable text into a custom view?
Then the answer is Yes, if your custom view extends ViewGroup
class.
However, if you want to draw specific graphics or animations with spannable, you would need to draw on the Canvas
directly from your custom view.
For examples, if you check DynamicDrawableSpan
class in android.text.style
package.
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
@IntRange(from = 0) int start, @IntRange(from = 0) int end, float x,
int top, int y, int bottom, @NonNull Paint paint) {
Drawable b = getCachedDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
} else if (mVerticalAlignment == ALIGN_CENTER) {
transY = (bottom - top) / 2 - b.getBounds().height() / 2;
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
It has draw
method which draw the Drawable
on the Canvas
directly, this code will be good start if you want to create custom spannable class which displays various things more than existing SDK classes.