7

Is there a way we can set a custom drawable (from resources) to a span, especially a ClickSpan, of TextView?

Google returns many answers for disabling highlights or changing colors etc by overriding updateDrawState() of the span, but I do not see how one can set a drawable to the background.

I see a possibility in DynamicDrawableSpan, but I am unable to make it work along with ClickableSpan. This is my code:

public class MyDynamicDrawableSpan extends DynamicDrawableSpan 
{
    private Context c;

public MyDynamicDrawableSpan(Context context) {
    super();
    c = context;
}

@Override
public Drawable getDrawable() {
    Resources res = c.getResources();
    Drawable d = res.getDrawable(R.drawable.span_background);
    return d;
}

}

And this is how I use it:

SpannableStringBuilder ssb = new SpannableStringBuilder(text);
MyDynamicDrawableSpan ddSpan = new MyDynamicDrawableSpan(getApplicationContext());
...
ssb.setSpan(ddSpan, start, end, 0);
ssb.setSpan(new ClickableSpan(...

This does not work. It only helps to make the span invisible. Any bright ideas?

GreenBee
  • 3,101
  • 3
  • 19
  • 18
  • what does your span_background.xml file look like? I have tried something similar by creating a shape drawable, but the span displays as invisible. Is this because a shape drawable must be set at the background of a view like a button? http://stackoverflow.com/questions/6674657/how-change-button-shape-using-xml-or-using-java-code – Etienne Lawlor May 28 '12 at 21:48
  • I am doing something similiar HERE http://stackoverflow.com/questions/10812316/contact-bubble-edittext – Etienne Lawlor May 30 '12 at 08:07
  • here i have explained it well. You can see my [answer]( http://krishnalalstha.wordpress.com/2012/06/07/making-gosmsproevernote-like-edittext/) – Krishna Shrestha Jun 07 '12 at 12:12

1 Answers1

0

You need to set the Bounds of the Drawable before returning it in getDrawable().

d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
monxalo
  • 581
  • 5
  • 14