Android Question selecting words or portion of texts.

aeropic

Active Member
Licensed User
Longtime User
Hi All,

I am searching for a lib allowing to manage text selections in a view (list view or label or anything else) without giving access to the keyboard.

My intention is to develop a small app based on text to speech to teach reading to chidren.
I just would want to display a text in a scrollable view and allow the child to tap on a word (or a set of words) to select it then read it with TTS. I just need the selection but I don't want to allow the keyboard to pop up ...

What would be the easiest way to do it ?

Thanks
Alain
 

warwound

Expert
Licensed User
Longtime User
You might be able to do this by displaying your text in a WebView.

Take a look at this page i created a while ago for another forum member: http://code.martinpearman.co.uk/deleteme/cbanks/highlight_on_click.php.
Click a paragraph and it gets highlighted, click again and it is un-highlighted.

Each paragraph of text is an HTML div element and the webpage javascript detects clicks on paragraphs and can send the highlighted paragraphs to a b4a Sub.

You want to detect clicks on single words...

Do you have control over the webpage HTML content - can you modify the webpage so that each word you want to be able to detect a click on is contained within an HTML span element?
If so it'd be trivial to adapt the code from the above link to detect clicks on span elements instead of div elements.
What i'm saying here is that you take text such as:

PHP:
Aenean sed mi justo, commodo imperdiet urna.

And modify it to:

PHP:
<span>Aenean</span> <span>sed</span> <span>mi</span> <span>justo</span>, <span>commodo</span> <span>imperdiet</span> <span>urna</span>.

The webpage javascript can now detect which span element has been clicked, get the span element's text and send it to a b4a Sub.

If however you have no control over the HTML content - say you load a 3rd party webpage hosted on the www - then you would have to excute some javascript that would find each and every word in the HTML and wrap it in a span element on the fly.
That's not impossible but you might end up wrapping words in spans where you don't want to detect clicks.

I just noticed you might also want to detect clicks on portions of text - that gets tricky.
Though if the portion of text was within a single span element (not multiple span elements) it'd be just as straightforward as detecting a click on a single word.

Martin.
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
thanks Martin and Erel, although not exactely what I searched, it is already a very good start ;)

From the example given in the librairy, I understand I need to wrap each word into an HTML tag (like <b>): OK. This would allow me to easily answer to the first need ie clicking on a single word would read it.

Now if I want to allow the app to read also complete sentences, I would need to wrap each single sentence into another tag (eg <i>)

Once having done this, how would I discriminate a click on a word <b> from a click on a sentence <i> ?
<i> <b>this</b> <b>is</b> <b>my</b> <b>sentence</b> </i>

Maybe an idea: is the librairy able to distinguish long click from short ones ? I could affect lo,g click to the complete sentence and short one to the words...
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The ClickableSpan object only raises the Click event - it doesn't raise a LongClick event - so that's not an option.

Martin.
 
Upvote 0

Petrus

Member
Licensed User
Longtime User
It would go with "RtfLabel". The selected text is passed in the event "MarkComplete":
B4X:
Sub RtfLabel1_MarkComplete(MarkText As String)
    'MarkText = text without formatting characters.
  
    'Read selected text: (MarkText)
    '...
  
End Sub

Regards
Petrus
 
Upvote 0

Petrus

Member
Licensed User
Longtime User
Yes, {pts/1.3} applies only to one §
B4X:
    'Sets the default font size for the display text:
    Rtf1.TextSize = 20

Regards
Petrus
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
Thanks Petrus, Here is the actual state of my book for kid small application.
I must admit that your lib + TTS Erel's one do 99% of the job !
I will improve the text sizing based on your previous answer and also add some directory management in order to allow to import books without recompiling, but the proof of concept is already a success.

Thanks to you all (including Martin) for your help.
Alain

PS : the book is in french trying to read it in German must be great !!!
 

Attachments

  • book4kid.zip
    343.1 KB · Views: 261
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
Petrus, is there a way to set the path of images ?
{pimage/fleurs.jpg/300/320}
To be more accurate, where is supposed to be located fleurs.jpg to be displayed with pimage tag ?
I would want to avoid to have to upload all the image files with the application. My idea is to browse for a txt file (the book to read) then collect, from the file dialog , the path and the name of the text file, then use the same path for the images... Is it possible ?
 
Last edited:
Upvote 0

Petrus

Member
Licensed User
Longtime User
I do not know if that is possible - start a new thread for the question.
In my apps I have all the files in the subfolder "Files"
Regards
Petrus
 
Upvote 0
Top