5

Is it possible to use selector drawable in ImageSpan?

I have TextView with selector text color, and add ImageSpan with selector resource. Text color change works fine, and image does not changes. Is it a bug or there is a way to make selector spans?

My TextView:

<TextView
    android:id="@+id/text"
    android:ellipsize="end"
    android:layout_height="fill_parent"
    android:duplicateParentState="true" // parent of this text view can be selected
    android:gravity="center_vertical"
    android:lines="1"
    android:textColor="@color/message_content_simple_text" // here is selector - it works fine
    android:textSize="15dp" />

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_active" android:state_focused="true"/>
    <item android:drawable="@drawable/ic_active" android:state_selected="true"/>
    <item android:drawable="@drawable/ic_active" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_passive"/>
</selector>

And code of adding span:

SpannableString text = new SpannableString("  some text");
ImageSpan span = new ImageSpan(context, R.drawable.icon, ImageSpan.ALIGN_BASELINE);
text.setSpan(span, 0, 1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(text);

When I select this text view "some text" change it's color as necessary, but image does not change.

EDIT

If there is some android bug, could you offer any way to achieve the same result?

2

2 Answers 2

9
+50

This isn't really a bug in Android, but more a limitation of using spans. Spans are meant for some basic text styling and not so much for (complex) user interaction that may or may not involve states and such. Basically, you should consider anything inside a span to be static. The fact that a selector works on all of the text inside a TextView is due to the TextView itself and has nothing to do with the underlying spans.

The reason a selector on an ImageSpan is not working is simply due to the fact that an ImageSpan is not state aware, and because there is nothing that notifies it of any state changes. That's perhaps what you could consider to be the responsibility of a TextView, but on the other hand, ask yourself the following question: should you really want a TextView to iterate over all its different spans to notify them of state updates?

That being said, it is definitely possible to modify (probably) both ImageSpan and TextView to enable state awareness on the underlying spans, but it will take some doing. The best next thing is to use the compound drawable feature of TextView, which allows you to position up to four drawables at the left, top, right or bottom inside a TextView (but not inline). A quick glance at the source code suggests compound drawables are state aware.

On a completely different note: I once answered a question that relates to yours. It dealt with making an animated GIF animate inside an ImageSpan. It's definitely not pretty, but it might help you understand the bit about not being state aware. Also, if you decide to go ahead with the implemention on your own issue, perhaps it'll be able to give you some pointers/ideas.

2
  • Thanks for reply. I thought about adding images to left of text view, but in my case it should be inline, so this is not an option.
    – Jin35
    Jul 5, 2012 at 5:11
  • Just to clarify - "compound drawables" are indeed state aware and works like a charm. Unfortunately you can't control there position as well as one might have hoped...
    – slott
    Jan 24, 2019 at 17:01
0

I haven't tested but it should be working...

Implement onTouch of TextView and handle ACTION_UP and ACTION_DOWN on it... simple and easy.

1
  • This is "hand made" solution. I think there should be some common way to do this on any text view
    – Jin35
    Jul 4, 2012 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.