I have a TextView in which every word is a ClickableSpan . When clicked, the word becomes bold , and the dictionary phrase is shown in another TextView. The application works correctly until I make the text in the TextView available. When the text is made selected, the definition is displayed by clicking, but the word is highlighted in bold with a double click. The text is selected by double-clicking or long pressing (but long pressing does not make the word bold).
I suppose the problem is when the drawing state is updated during the processing of actions, but I could not find a fix. I tried to set the TextView focusable="false" , but nothing changed. The corresponding code is below.
curSpan = new WordSpan(index) { @Override public void onClick(View view) { handleWordClick(index,this);
And the definition of WordSpan:
class WordSpan extends ClickableSpan { int id; private boolean marking = false; public WordSpan(int id) { this.id = id; } @Override public void updateDrawState(TextPaint ds) { ds.setColor(Color.BLACK); ds.setUnderlineText(false); if (marking) { ds.setTypeface(Typeface.create(myFont,Typeface.BOLD)); } } @Override public void onClick(View v) {} public void setMarking(boolean m) { marking = m; } }
Setting the move method for TextView:
private MovementMethod createMovementMethod ( Context context ) { final GestureDetector detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp ( MotionEvent e ) { return true; } @Override public boolean onSingleTapConfirmed ( MotionEvent e ) { return true; } }); return new ScrollingMovementMethod() { @Override public boolean canSelectArbitrarily () { return true; } @Override public void initialize(TextView widget, Spannable text) { Selection.setSelection(text, text.length()); } @Override public void onTakeFocus(TextView view, Spannable text, int dir) { if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) { if (view.getLayout() == null) {
Edit: I just discovered a new fad that can help with the solution. If I double-clicked, but changed the words between clicks (tap one word and then another word quickly), the first press will show the definition for that word, and the second touch the word FIRST is shown in bold, but SECOND word is selected (highlighted) and the definition for the FIRST word is still shown.
So, for example, if I double-click “first” and then “second”, when I press “first” the definition for “first” will be shown, and when I touch “second” the word “first” is shown in bold and the word “ second "is highlighted but the definition does not change (the definition for" First "is still shown).