1

I want to display text (relatively large) and determine which character the user touches. I'd also like to embellish the text by drawing a circle (for example) over certain characters. How do I know which characters are at what location? Enforcing monospaced font would be fine if helpful programmatically.

Would it also be able to make this one line of text scroll left/right (using touch->slide) and still have touch work to determine the correct character?

Joel
  • 2,601
  • 4
  • 33
  • 44

1 Answers1

1

A very broad question, and you've got a lot of work ahead, but here's the outline of one possible solution:

  • Create a custom view. ImageView is often a good base class to inherit.

    Override the onDraw() method of the view.

    Use canvas.drawText() to place your text on screen.

    Use paint.getTextBounds to meausure your text. See the discussion linked below for a thorough understanding. Since you drew the text on screen, and you measured it, you know exactly where each character is.

    Override the onTouch() method in your custom view and track the events you need. There are many QAs here in SO on how to do this.

    For scrolling the text, use canvas.translate() using the movement you calculate in the onTouch(). Since you know how much you scrolled (translated), you know how far your characters have moved and can offset your touch detection to detect character touches.

    Finally, since you now control all of the drawing and, your character positions are abstract, only having meaning when compared to a touch position, you can embellish them in any way choose.

Android Paint: .measureText() vs .getTextBounds()

Phew!

Good luck.

Community
  • 1
  • 1
Simon
  • 14,407
  • 8
  • 46
  • 61
  • I haven't gotten started on this, but I did try to "survey" the possible options. Your answer seems to be in line with the appropriate solution, so marking as the answer. The only part I think I may diverge is using ScrollView instead of implementing translation myself. I'm hoping touches will still give appropriate custom view/canvas coords inside the scroll view. I plan to report back here what works for me. – Joel Sep 25 '12 at 03:35