Android - Setting custom drawable background to ClickSpan
Asked Answered
V

1

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?

Viand answered 13/11, 2011 at 5:57 Comment(3)
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? #6675157Karankaras
I am doing something similiar HERE https://mcmap.net/q/234945/-contact-bubble-edittextKarankaras
here i have explained it well. You can see my How to Answer( krishnalalstha.wordpress.com/2012/06/07/…)Psychotic
A
0

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

d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
Azevedo answered 16/12, 2011 at 12:55 Comment(2)
I tried this but it still doesn't work for an xml defined shape drawable. The span is just invisible.Karankaras
The clickablespan must be set like this: #8641843Karankaras

© 2022 - 2024 — McMap. All rights reserved.