Android Code Snippet Label Utils

klaus

Expert
Licensed User
SubName: SetTextSize

Description: Sets the text size for a given text to fill in a given Label or EditText view.

B4X:
Sub Globals
  Dim stu As StringUtils
End Sub

Sub SetTextSize(lbl As Label, txt As String)
  Dim dt As Float
  Dim limit = 0.5 As Float
  Dim h As Int

  lbl.Text = txt
  lbl.TextSize = 72
  dt = lbl.TextSize
  h = stu.MeasureMultilineTextHeight(lbl, txt)
  Do While dt > limit OR h > lbl.Height
    dt = dt / 2
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    If h > lbl.Height Then
      lbl.TextSize = lbl.TextSize - dt
    Else
      lbl.TextSize = lbl.TextSize + dt
    End If
  Loop
End Sub
Tags: Label, TextSize, EditText, text size

Dependencies: StringUtils library

Usage for the image below:
B4X:
Dim txt = "This is the time for all good men to come to the aid of their country" As String

SetTextSize(Label1, txt)
SetTextSize(Label2, txt)
SetTextSize(Label3, txt)



Original post.
 

Attachments

Last edited:

klaus

Expert
Licensed User
Subject: Ellipsizing and Marquee text

In this post you find several routines to ellipsize and marquee texts in Labels.
What does ellipsize mean ? Look at the image.
MARQUEE means horizontal scrolling of the text.

START, MIDDLE and END work also with multiline Labels
MARQUEE works only in a single line Label

Sub: setEllipsize
Description: sets the ellipsizing of text
B4X:
'Sets the Ellipsizing of a TextView (Label, EditText)
' Mode can be
'    original text 'This is a test text'            
'"START"          ... This is a te
'"MIDDLE"        This is ... text
'"END"             This is a tes...
'"MARQUEE      This is a test t
Sub setEllipsize(TextView As Label, Mode As String)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setSingleLine", Array As Object(True))
    jo.RunMethod("setEllipsize", Array As Object(Mode))
    jo.RunMethod("setSelected", Array As Object(True))  ' needed for MARQUEE
End Sub

Sub: resetEllipsize
Description: removes ellipsizing
B4X:
'Removes the Ellipsizing of a TextView (Label, EditText)
Sub resetEllipsize(TextView As Label)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setSingleLine", Array As Object(False))
End Sub

Sub: setSingleLLine
Description: sets the Label to single line and allows scrolling of text
B4X:
'Sets the TextView to single line
Sub setSingleLine(TextView As Label, SingleLine As Boolean)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setSingleLine", Array As Object(SingleLine))
End Sub

Sub: setLines
Description: sets the line number of the Label
B4X:
'Limits the line number to the given value
Sub setLines(TextView As Label, LineNumber As Int)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setLines", Array As Object(LineNumber))
End Sub

Sub: setMarqueeRepeat
Description: sets the number of scrolling
B4X:
'Sets the repeat limit
' default is three times
' set -1 for repeat indefinitely
Sub setMarqueeRepeatLimit(TextView As Label, Limit As Int)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setMarqueeRepeatLimit", Array As Object(Limit))
End Sub
The JavaObject routines can also be used without a sub like this:
B4X:
Private jobj As JavaObject
Private lblTest As Label
'
'
jobj = lblTest
jobj.RunMethod("setLines", Array As Object(2))
jobj.RunMethod("setEllipsize", Array As Object("END"))
Stop or run the horizontal scrolling can be done with:
B4X:
jobj.RunMethod("setHorizontallyScrolling", Array As Object(False))
'or
jobj.RunMethod("setHorizontallyScrolling", Array As Object(True))
Dependencies: JavaObject library

Tags: Label, TextView, ellipsize, START, MIDDLE, END, MARQUEE, shadow

Attached a small test program.
 

Attachments

Last edited:

klaus

Expert
Licensed User
Subject: Add a shadow to a text in a Label or EditText view

This routine adds a shadow to a TextView (Label, or EditText).

Sub: setShadowLayer
Description: sets a shadow to the text

B4X:
'Adds a shadow to a text
'lbl = the view to add a shadow, can be and EditText or a Label
'Radius = parameter for blur, radius = 1 no blur, the bigger radius the more blur
'dx = horizontal offset of the shadow in pixels
'dy = vertical offset of the shadow in pixels
'Color = shadow color
Sub setShadowLayer(lbl As View, Radius As Float, dx As Float, dy As Float, Color As Int)
    Dim jo = lbl As JavaObject
    jo.RunMethod("setShadowLayer", Array(Radius, dx, dy , Color))
End Sub
Dependencies: JavaObject library

Tags: Label, TextView, text, shadow

Attached a small test program.
 

Attachments

KMatle

Expert
Licensed User
Description: Sets the text size for a given text to fill in a given Label
I've used that example with a Edittext. In the thread is was a label only. Any reasons for it? Using the search for "Textsize" will lead members to this and some of them might think "damned... this one is for labels only" :(
 

Gabino A. de la Gala

Active Member
Licensed User
Thanks for help, klaus.

This is very useful for me.

Can I set the speed of horizontal scrolling?
 

Bryan

Member
Licensed User
SubName: SetTextSize

Description: Sets the text size for a given text to fill in a given Label.

B4X:
Sub Globals
  Dim stu As StringUtils
End Sub

Sub SetTextSize(lbl As Label, txt As String)
  Dim dt As Float
  Dim limit = 0.5 As Float
  Dim h As Int

  lbl.Text = txt
  lbl.TextSize = 72
  dt = lbl.TextSize
  h = stu.MeasureMultilineTextHeight(lbl, txt)
  Do While dt > limit OR h > lbl.Height
    dt = dt / 2
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    If h > lbl.Height Then
      lbl.TextSize = lbl.TextSize - dt
    Else
      lbl.TextSize = lbl.TextSize + dt
    End If
  Loop
End Sub
Original post.
Klaus, can this be adapted somehow for use with a label in a widget?

Thanks,
Bryan
 

RandomCoder

Well-Known Member
Licensed User
You could have a look at this ScrollingLabels :
Would you say that the Marquee method of scrolling text is more efficient as its using a native java object? I'm currently using the ScrollingLabels code and had to modify my own code to reduce memory consumption. Im just wondering if it would be possible to display more Marquee labels than you currently can with ScrollingLabels?

Thanks,
RandomCoder
 

klaus

Expert
Licensed User
Would you say that the Marquee method of scrolling text is more efficient as its using a native java object?
I don't know.
ScrollingLabels uses a Canvas, drawing the text, for the scrolling effect.

You can add the Marquee effect to any Label.
 
Last edited:
Top