Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce ImageSpan height and width

I'm setting an Image Drawable as a SpannableString to a TextView but the image comes out larger than the text making it look weird. I need to reduce the size of the imagespan such that it's the same height as the text:

Here's what I've tried:

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

SpannableStringBuilder builder = new SpannableStringBuilder(holder.temptext.getText());
builder.setSpan(new ImageSpan(drawable), selectionCursor - ":)".length(), selectionCursor, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.temptext.setText(builder);
holder.temptext.setSelection(selectionCursor);

holder.caption.setText(builder);
like image 214
Dinuka Jay Avatar asked Aug 30 '15 16:08

Dinuka Jay


1 Answers

You need to measure your TextView's height and use it instead of intrinsic bounds:

int lineHeight = holder.temptext.getLineHeight();

drawable.setBounds(0, 0, lineHeight, lineHeight);
like image 78
Nikolai Avatar answered Oct 06 '22 12:10

Nikolai