Addev
Addev

Reputation: 32243

Inline images in a TextView

I'm trying to put inline images in a TextView. Here goes my code:

ImageGetter imageGetter = new ImageGetter() {
    public Drawable getDrawable(String source) {
        Drawable d = ctx.getResources().getDrawable(Integer.parseInt(source));
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
};
CharSequence votesString="124 votes";
votesString=Html.fromHtml(votesString
           +"(<img src='" + R.drawable.icon_upvotes + "'/>)", imageGetter, null);
labelVotes.setText(votesString);

It works (see the image below) but I would need the image to be vertically centered. How can I do that?

enter image description here

Thanks

Upvotes: 0

Views: 278

Answers (2)

Manuel Spigolon
Manuel Spigolon

Reputation: 12900

Why you don't use android:drawableRight ?

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157457

Instead of use HTML, you can use

labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);

and playing a little bit with padding, using setCompoundDrawablePadding

Upvotes: 4

Related Questions