Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE:</strong> Another better approach is to use <code>BreakIterator</code>:</p> <pre><code>private void init() { String definition = "Clickable words in text view ".trim(); TextView definitionView = (TextView) findViewById(R.id.text); definitionView.setMovementMethod(LinkMovementMethod.getInstance()); definitionView.setText(definition, BufferType.SPANNABLE); Spannable spans = (Spannable) definitionView.getText(); BreakIterator iterator = BreakIterator.getWordInstance(Locale.US); iterator.setText(definition); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator .next()) { String possibleWord = definition.substring(start, end); if (Character.isLetterOrDigit(possibleWord.charAt(0))) { ClickableSpan clickSpan = getClickableSpan(possibleWord); spans.setSpan(clickSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } private ClickableSpan getClickableSpan(final String word) { return new ClickableSpan() { final String mWord; { mWord = word; } @Override public void onClick(View widget) { Log.d("tapped on:", mWord); Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT) .show(); } public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); } }; } </code></pre> <hr> <p><strong>OLD ANSWER</strong></p> <p>I wanted to handle click in my own Activity. I solved it by following code:</p> <pre><code>private void init(){ String definition = "Clickable words in text view ".trim(); TextView definitionView = (TextView) findViewById(R.id.definition); definitionView.setMovementMethod(LinkMovementMethod.getInstance()); definitionView.setText(definition, BufferType.SPANNABLE); Spannable spans = (Spannable) definitionView.getText(); Integer[] indices = getIndices( definitionView.getText().toString(), ' '); int start = 0; int end = 0; // to cater last/only word loop will run equal to the length of indices.length for (int i = 0; i &lt;= indices.length; i++) { ClickableSpan clickSpan = getClickableSpan(); // to cater last/only word end = (i &lt; indices.length ? indices[i] : spans.length()); spans.setSpan(clickSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = end + 1; } } private ClickableSpan getClickableSpan(){ return new ClickableSpan() { @Override public void onClick(View widget) { TextView tv = (TextView) widget; String s = tv .getText() .subSequence(tv.getSelectionStart(), tv.getSelectionEnd()).toString(); Log.d("tapped on:", s); } public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); } }; } public static Integer[] getIndices(String s, char c) { int pos = s.indexOf(c, 0); List&lt;Integer&gt; indices = new ArrayList&lt;Integer&gt;(); while (pos != -1) { indices.add(pos); pos = s.indexOf(c, pos + 1); } return (Integer[]) indices.toArray(new Integer[0]); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload