I've used in textview thus:
SpannableStringBuilder ssb = new SpannableStringBuilder( "My String contains one icon." );
Bitmap fvorito = new IconicsDrawable(getContext(), FontAwesome.Icon.faw_star_o)
.sizeDp(25)
.color(ContextCompat.getColor(getContext(), R.color.md_blue_grey_300))
.toBitmap();
//ImageSpan imageSpan = new ImageSpan(null, fvorito, ImageSpan.ALIGN_BOTTOM);
ImageSpan imageSpan = new CenteredImageSpan(null, fvorito, ImageSpan.ALIGN_BOTTOM);
ssb.setSpan( imageSpan, 10, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
txtDescricao.setText( ssb, TextView.BufferType.SPANNABLE );
I do not know if I could help you
CenteredImageSpan is class
public class CenteredImageSpan extends ImageSpan {
//http://stackoverflow.com/questions/25628258/align-text-around-imagespan-center-vertical
public CenteredImageSpan(Context context, Bitmap b, int verticalAlignment) {
super(context, b, verticalAlignment);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
Drawable b = getDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
// this is the key
transY -= paint.getFontMetricsInt().descent / 2;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
Output:
