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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With