Android Question Long text length(single Line) fit label width automatically

Alhootti

Member
I have a label i want to use long text through it, the problem right now i should increase the width of label manually.
Is there a way to increase the label's width automatically to occupies the length of text ?( single line only not multi line)
even the text come out from the layout.
label.text = " Orthodontists are concerned with the study and treatment of malocclusions (improper bites), which may be a result of tooth irregularity and/or disproportionate jaw relationships. .......................................................................................................... "
 

Juanet

Member
Licensed User
Longtime User
Maybe this can help you:
 
Upvote 0

Alhootti

Member
Based on the example i got this Error:
Main - 71: Syntax error.
Main - 77: Undeclared variable 'cvs1' is used before it was assigned any value.
Main - 76: Undeclared variable 'centery' is used before it was assigned any value.
Main - 75: Undeclared variable 'cvs1' is used before it was assigned any value.
 
Upvote 0

Alhootti

Member
I'm using this code to get solution for my topic but it need more development.
any body can help me to get horizontal extending automatically that fit length of words.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Dim lbl_Hor_length As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Label3.Text = "123456789AB A hfhdhfdhh  123456789  944436644444444 12444554454455 4444777777  Alhootti 0123456789  ALHOOTTI www.b4x.com"
    lbl_Hor_length = Label3.Text.Length * 29
    Label3.Width = lbl_Hor_length
End Sub
 
Upvote 0

Serge Bertet

Active Member
Licensed User
Look inside AutoTextSizeLabel.bas class

something using StringUtils
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do it that way:
- Use a HorizontalScrollView allowing to scroll the text.
- Put the Label onto the HorizontalScrollView.
- When adding text, calculate the text width and set the Label.Width and the HorizontalScrollView.Panel.Width.

Attached my test program. The layout contains just the HorizontalScrollView.
If you need several Labels like this one it could be interesting to make a CustomView.

And the code:

B4X:
Sub Globals
    Private HorizontalScrollView1 As HorizontalScrollView
    Private Label1 As Label
    Private bmpTest As Bitmap
    Private cvsTest As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    InitCanvas
    InitLabel
   
    AddText("123456789AB A hfhdhfdhh  123456789  944436644444444 12444554454455 4444777777  Alhootti 0123456789  ALHOOTTI www.b4x.com")
End Sub

Private Sub InitCanvas
    'Inintialize a small Bitmap for the Canvas
    bmpTest.InitializeMutable(2dip, 2dip)
    cvsTest.Initialize2(bmpTest)
End Sub

Private Sub InitLabel
    'Initialize the Label
    Label1.Initialize("")
    Label1.TextSize = 18

    'Add the Label onot the HorizontalScrollView
    HorizontalScrollView1.Panel.AddView(Label1, 0, 0, 100dip, HorizontalScrollView1.Height)

    'Remove the horizontal scrollbar
    Private jo = HorizontalScrollView1 As JavaObject
    jo.RunMethod("setHorizontalScrollBarEnabled", Array As Object(False))
End Sub

Private Sub AddText(Text As String)
    Label1.Text = Text

    'Calculate the width
    Label1.Width = cvsTest.MeasureStringWidth(Label1.Text, Label1.Typeface, Label1.TextSize)
    HorizontalScrollView1.Panel.Width = Label1.Width
End Sub

@Serge Bertet
StringUtils has no MeasureStringWidth method ! Only MeasureMultilineTextHeight.
A Canvas has the MeasureStringWidth.
 

Attachments

  • LabelLongText.zip
    9.2 KB · Views: 68
Last edited:
Upvote 0

Intelemarketing

Active Member
Licensed User
Longtime User
Not sure if B4X has a property called something like ToolTipText (as per VB6)

When I need to view an extra long field , be it a Label or Text Field, I put the value into the ToolTipText Property and it can be viewed by just hovering over the original Label or Text Field.
 
Upvote 0
Top