I finally found a trick that works for me. When the color of text in a TextView changes, all the highlights are reset. Therefore, if I cause a change in the color of the text in the TTS callback, then the selection is deleted. The dirty part is that the triggered color change must be a different color. Therefore, I have to change colors both during TTS access and in the onClick handler from ClickableSpan. And I set these two colors to two almost identical colors.
My ClickableSpan:
final int AlmostBlack = m_Resources.getColor(R.color.i_black_almost); ClickableSpan readWord = new ClickableSpan() { private int almostBlack = AlmostBlack; public void onClick(View view) { TextView v = (TextView) view; v.setTextColor(almostBlack); ...
And in the handler, when the TTS calls back:
m_SentenceView.setTextColor(m_Resources.getColor(R.color.i_black));
If you want to do something like this, but without waiting for the TTS or something to call back, you can use the list of color states to trigger a color change when you click or release the view:
List of color states, res / color / clickable_words.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="@color/i_black_almost" android:state_pressed="true"/> <item android:color="@color/i_black" /> </selector>
TextView:
<TextView android:id="@+id/sentence" ... android:textColor="@color/clickable_words" android:textColorLink="@color/clickable_words" android:textColorHighlight="@color/i_blue" />
source share