B4A Library LabelExtras - advanced click handling

LabelExtras enables you to apply click listeners to sub-strings of a Label text.
A click on a sub-string raises an event and you can take whatever action you desire on such a click.

LabelExtras also exposes the Android Html class so you can use basic HTML formatting elements such as <b>, <i> and <u> in your Label text.

Here's the library reference:

LabelExtras
Version:
1.12
  • ClickableSpan
    Events:
    • Click (SelectionStart As Int, SelectionEnd As Int)
    • Click2 (SelectionStart As Int, SelectionEnd As Int, Tag As Object)
    Methods:
    • Initialize (EventName As String, Color As Int, UnderlineText As Boolean)
      Initialize the ClickableSpan.
      Color defines the color of the clickable text, pass -1 if you do not wish to change that text color.
      UnderlineText defines whether the clickable text shall be underlined.
      The Click(SelectionStart As Int, SelectionEnd As Int) event will be raised when this ClickableSpan is clicked.
    • Initialize2 (EventName As String, Color As Int, UnderlineText As Boolean, Tag As Object)
      Initialize the ClickableSpan.
      Color defines the color of the clickable text, pass -1 if you do not wish to change that text color.
      UnderlineText defines whether the clickable text shall be underlined.
      Pass any Object as the Tag and this Object will be returned to the Click2 event.
      The Click2(SelectionStart As Int, SelectionEnd As Int, Tag As Object) event will be raised when this ClickableSpan is clicked.
    • IsInitialized As Boolean
  • Html
    Events:
    • GetDrawable (Source As String) As Object
    • HandleTag (Opening As Boolean, Tag As String, SpannableStringBuilder1 As SpannableStringBuilder)
    Methods:
    • EscapeHtml (CharSequence1 As CharSequence) As String
      Returns an HTML escaped representation of the given plain text.
      Only available with Android AP version 16+.
    • FromHtml (HtmlString As String) As CharSequence
      Returns displayable styled text from the provided HTML string.
    • FromHtml2 (HtmlString As String, ImageGetterEventName As String, TagHandlerEventName As String) As CharSequence
      Returns displayable styled text from the provided HTML string.
      Using optional ImageGetter (to handle HTML IMG tags in the String) and TagHandler (to handle unrecognised tags in the String) callbacks.
      The drawable returned by the ImageGetter sub has to be sized with setBounds.
    • ToHtml (CharSequence1 As CharSequence) As CharSequence
      Returns an HTML representation of the provided Spanned text.
  • LabelExtras
    Methods:
    • SetDrawableBounds (Drawable1 As Drawable, Left As Int, Top As Int, Right As Int, Bottom As Int)
      Specifies a bounding rectangle for the drawable.
      Required for the Html Object ImageGetter callback.
    • SetMovementMethod (Label1 As TextView, MovementMethod1 As MovementMethod)
      Sets the movement method (arrow key handler) to be used for this TextView.
      To enable the LabelExtras click functionality you must set your Label movement method to an instance of LinkMovementMethod.
      The LinkMovementMethod.GetInstance method returns the instance required.
  • LinkMovementMethod
    Methods:
    • GetInstance As MovementMethod
      Returns an instance of the LinkMovementMethod which is required by the LabelExtras.SetMovementMethod method.
  • SpannableStringBuilder
    Methods:
    • Append (Text As CharSequence) As SpannableStringBuilder
    • Append2 (Text As CharSequence, Start As Int, End As Int) As SpannableStringBuilder
    • Append3 (Char1 As Char) As SpannableStringBuilder
    • CharAt (Where As Int) As Char
    • Clear
    • ClearSpans
    • Delete (Start As Int, End As Int) As SpannableStringBuilder
    • GetChars (Start As Int, End As Int, Dest() As Char, DestOffset As Int)
    • GetSpanEnd (Span1 As Object) As Int
    • GetSpanFlags (Span1 As Object) As Int
    • GetSpanStart (Span1 As Object) As Int
    • GetStyleSpans (QueryStart As Int, QueryEnd As Int) As StyleSpan[]
    • GetURLSpans (QueryStart As Int, QueryEnd As Int) As URLSpan[]
    • GetUnderlineSpans (QueryStart As Int, QueryEnd As Int) As UnderlineSpan[]
    • Initialize (CharSequence1 As CharSequence)
      Initialize the SpannableStringBuilder with a CharSequence.
      A CharSequence can be obtained using the Html.FromHtml method, a String can also be passed as a CharacterSequence.
    • IsInitialized As Boolean
    • NextStyleSpanTransition (Start As Int, Limit As Int) As Int
      Return the next offset after Start but less than or equal to Limit where a StyleSpan begins or ends.
    • NextURLSpanTransition (Start As Int, Limit As Int) As Int
      Return the next offset after Start but less than or equal to Limit where a URLSpan begins or ends.
    • NextUnderlineSpanTransition (Start As Int, Limit As Int) As Int
      Return the next offset after Start but less than or equal to Limit where an UnderlineSpan begins or ends.
    • RemoveSpan (Span1 As Object)
    • Replace (Start As Int, End As Int, CharSequence1 As CharSequence) As SpannableStringBuilder
    • Replace2 (Start As Int, End As Int, CharSequence1 As CharSequence, CharSequence1Start As Int, CharSequence1End As Int) As SpannableStringBuilder
    • SetSpan (Span1 As Object, Start As Int, End As Int, Flags As Int)
    Properties:
    • Length As Int [read only]
      Return the number of Chars in the buffer.
  • Spanned
    Fields:
    • SPAN_EXCLUSIVE_EXCLUSIVE As Int
      Constant which can be passed as the SpannableStringBuilder.SetSpan method Flags parameter.
      Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point.
    • SPAN_EXCLUSIVE_INCLUSIVE As Int
      Constant which can be passed as the SpannableStringBuilder.SetSpan method Flags parameter.
      Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand to include text inserted at their ending point but not at their starting point.
    • SPAN_INCLUSIVE_EXCLUSIVE As Int
      Constant which can be passed as the SpannableStringBuilder.SetSpan method Flags parameter.
      Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their starting point but not at their ending point.
    • SPAN_INCLUSIVE_INCLUSIVE As Int
      Constant which can be passed as the SpannableStringBuilder.SetSpan method Flags parameter.
      Spans of type SPAN_INCLUSIVE_INCLUSIVE expand to include text inserted at either their starting or ending point.
  • StyleSpan
    Methods:
    • GetStyle As Int
      Returns the Typeface style of this StyleSpan.
      Typeface.STYLE_BOLD, Typeface.STYLE_ITALIC etc.
    • IsInitialized As Boolean
  • URLSpan
    Methods:
    • GetURL As String
    • IsInitialized As Boolean
  • UnderlineSpan
    Methods:
    • IsInitialized As Boolean

Example code to follow.

Martin.
 

Attachments

  • LabelExtras_v1_12.zip
    15.9 KB · Views: 1,833
Last edited:

warwound

Expert
Licensed User
Longtime User

Kwame Twum

Active Member
Licensed User
Longtime User
pls how do you get rid of the tags in the final output?
visible tags.jpg
 
Last edited:

warwound

Expert
Licensed User
Longtime User
pls how do you get rid of the tags in the final output?
View attachment 39065

Look up to post #61, only a subset of HTML tags are supported.
Unrecognised tags may or may not be displayed as plain text - this looks to be part of your problem.

The other problem may be that your HTML is not 100% valid - mismatched and/or incomplete tags may be displayed as plain text.
Validate your HTML: https://validator.w3.org
 

Kwame Twum

Active Member
Licensed User
Longtime User
Look up to post #61, only a subset of HTML tags are supported.
Unrecognised tags may or may not be displayed as plain text - this looks to be part of your problem.

The other problem may be that your HTML is not 100% valid - mismatched and/or incomplete tags may be displayed as plain text.
Validate your HTML: https://validator.w3.org

Thanks a lot @warwound for the reply
I made no modifications to tags from the project I downloaded from your post... all I did was to edit the image names and add the respective image files to the Files tab... and compiled.

:) However, I commented out the entire TagHandler_HandleTag block and it works just fine!
 

SJQ

Member
Licensed User
Longtime User
Hi @warwound

I have a question.

I am using Labels extra for displaying a training questionnaire, the student reads the text and then selects one of the options at the bottom. Great works as expected.

However the questions asked tend to be quite long and the reader has to scroll the text in the label, that too works great.

My problem is… That when I load the next question the scroll position remains where is was and does not reset back to the top.

I have to scroll it back to the top myself then read the question, scroll down again and tap the required answer.

Is there a way to reset the scroll position?

Thanks
Steve
 

SJQ

Member
Licensed User
Longtime User
Hi, Answered my own question, just removed the view and added it again.
 

Blue.Sky

Active Member
Licensed User
Longtime User
Hi
Can i custom selection in labelextra example Edittext?
I want to use can select part of text with drag
 

Blue.Sky

Active Member
Licensed User
Longtime User
Excuse me,how can i detect all word clicking?
 

scsjc

Well-Known Member
Licensed User
Longtime User
Nice library....
is possible get event when touch a non UrlSPAN,

or .... get event long click ???
 
Top