Here we have a working example...
I've created a new library object called LabelExtras:
LabelExtras
Version: 0.01
- LabelExtras
Events:
- UnderlineSpanClick (SelectionStart As Int, SelectionEnd As Int)
Methods:
- SetClickableText (Label1 As TextView, HtmlString As String, EventName As String)
Turns any <u> elements in HtmlString into clickable spans and sets HtmlString as the Label1 Text property.
If a clickable span is clicked then the UnderlineSpanClick(SelectionStart As Int, SelectionEnd As Int) event is raised.
And some example B4A code:
Sub Process_Globals
End Sub
Sub Globals
Dim ClickableLabel As Label
Dim LogLabel As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
' note that the SetClickableText method parses the String as HTML
' so if you try to use the CRLF constant in this String it will not create a new line
' instead the HTML <br> element can be used
Dim HtmlString As String="<u>Any</u> text that you want to detect clicks on must be within an HTML <u>UNDERLINE</u> element, the UnderlineSpanClick event is passed SelectionStart and SelectionEnd parameters.<br>Click <u>HERE</u> to continue."
Dim LabelExtras1 As LabelExtras
LabelExtras1.SetClickableText(ClickableLabel, HtmlString, "ClickableLabel")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ClickableLabel_UnderlineSpanClick(SelectionStart As Int, SelectionEnd As Int)
Dim LogText As StringBuilder
LogText.Initialize
LogText.Append("ClickableLabel_UnderlineSpanClick: "&DateTime.Now&CRLF)
LogText.Append("SelectionStar="&SelectionStart&CRLF)
LogText.Append("SelectionEnd="&SelectionEnd&CRLF)
Dim ClickedText As String=GetClickedText(SelectionStart, SelectionEnd)
LogText.Append("Clicked text='"&ClickedText&"'")
LogLabel.Text=LogText.ToString
If ClickedText="HERE" Then
StartActivity(ActivityTwo)
End If
End Sub
Sub GetClickedText(SelectionStart As Int, SelectionEnd As Int) As String
Dim LabelText As String=ClickableLabel.Text
Log("LabelText="&LabelText)
Dim ClickedText As String
' i've put this code in a Try/catch block as i was getting an occasional exception with an invalid range
' will need to be debugged...
Try
ClickedText=LabelText.SubString2(SelectionStart, SelectionEnd)
Catch
ClickedText=LastException
End Try
Return ClickedText
End Sub
The library parses any HTML <u> elements into clickable spans, if the user clicks one of these clickable spans then the UnderlineSpanClick event is raised and passed the start and end index of the clicked text.
In tests i got an occasional error when clicking the first clickable span - the span that conatins the word 'Any'.
The error was an out of range exception where the SelectionStart was -1 and the SelectionEnd was 0.
In theory this shouldn't occur but it needs a little debugging.
You'll probably not be happy with the default HTML syle applied to the clickable spans - on my tablet they are a pretty unreadable light blue color.
Different devices will probably render the clickable spans differently so a solution is required.
Give the attched code a test and post with your results.
Martin.