1

hi i have a sentence (TextView) and I want to tap a certain word and I want to show this word in Toast, e.g. I would like to set the OnClickLister to be able to grab a word, wich was tapped, not the whole sentence

for example

sentence TextView --- I love stackoverflow

tap on "love" -----> toast the word "love"

I've only come up with the idea of creating many TextViews, and set listners for each one...it can't be good... Do you have any ideas how to implement this?

maybe there are some libraries for word processing I'm not aware of?

tania
  • 1,086
  • 2
  • 12
  • 31

2 Answers2

3

select a word on a tap in TextView/EditText

see the above question. I think it may solve your problem. You need not have one textview for each word. Have one textview for the whole sentence. then catch the word clicked. I think that is wat is explained above.

Please let me know if this helped.

Community
  • 1
  • 1
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92
0

An alternate solution to setting a ClickableSpan for each word (as the reference in @LakshmiNarayanan's answer suggested) would be to set an OnTouchListener on the TextView. Then in the onTouch method you can get the offset of the character that has been touched like this:

Layout layout = this.getLayout();
if (layout != null)
{
    int line = layout.getLineForVertical(motionEvent.getY()); 
    int offset = layout.getOffsetForHorizontal(line, motionEvent.getX());
} 

After that you could use a BreakIterator or your own implementation to return the word that was touched.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393