B4J Question [Solved] B4XFloatTextField and HintLabelSmall. Adjusting to the right

Dadaista

Active Member
Licensed User
Longtime User
Hi Again! :)

I just Want to move the small hint label to the right at the end of B4XFloatTextField

Is there a way to get the width of the small hint label?... or any kind of formula to adjust at right of B4XFloatTextField using HintLabelSmallOffsetX

Thx
 

TILogistic

Expert
Licensed User
Longtime User
It may be a way to do it.

B4X:
B4XFloatTextField1.HintLabelSmallOffsetX = B4XFloatTextField1.TextField.Width -  MeasureTextWidth(B4XFloatTextField1.HintText,B4XFloatTextField1.HintFont) - 6

B4X:
'https://www.b4x.com/android/forum/threads/b4x-xui-add-measuretextwidth-and-measuretextheight-to-b4xcanvas.91865/#post-580637
Public Sub MeasureTextWidth(Text As String, Font1 As B4XFont) As Int
#If B4A
    Private bmp As Bitmap
    bmp.InitializeMutable(2dip, 2dip)
    Private cvs As Canvas
    cvs.Initialize2(bmp)
    Return cvs.MeasureStringWidth(Text, Font1.ToNativeFont, Font1.Size)
#Else If B4i
    Return Text.MeasureWidth(Font1.ToNativeFont)
#Else If B4J
    Dim jo As JavaObject
    jo.InitializeNewInstance("javafx.scene.text.Text", Array(Text))
    jo.RunMethod("setFont",Array(Font1.ToNativeFont))
    jo.RunMethod("setLineSpacing",Array(0.0))
    jo.RunMethod("setWrappingWidth",Array(0.0))
    Dim Bounds As JavaObject = jo.RunMethod("getLayoutBounds",Null)
    Return Bounds.RunMethod("getWidth",Null)
#End If
End Sub

Public Sub MeasureTextHeight(Text As String, Font1 As B4XFont) As Int
#If B4A  
    Private bmp As Bitmap
    bmp.InitializeMutable(2dip, 2dip)
    Private cvs As Canvas
    cvs.Initialize2(bmp)
    Return cvs.MeasureStringHeight(Text, Font1.ToNativeFont, Font1.Size)
#Else If B4i
    Return Text.MeasureHeight(Font1.ToNativeFont)
#Else If B4J
    Dim jo As JavaObject
    jo.InitializeNewInstance("javafx.scene.text.Text", Array(Text))
    jo.RunMethod("setFont",Array(Font1.ToNativeFont))
    jo.RunMethod("setLineSpacing",Array(0.0))
    jo.RunMethod("setWrappingWidth",Array(0.0))
    Dim Bounds As JavaObject = jo.RunMethod("getLayoutBounds",Null)
    Return Bounds.RunMethod("getHeight",Null)
#End If
End Sub

1613767527329.png


Spanish:
publique en el foro spanish, tambien le pueden ayudar.

Saludos,
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
move to start of radius

B4XFloatTextField1.HintLabelSmallOffsetX = B4XFloatTextField1.TextField.Width - MeasureTextWidth(B4XFloatTextField1.HintText,B4XFloatTextField1.HintFont) - B4XFloatTextField1.TextField.Height/2

You only have to test and you can find another solution


1613769992874.png
 
Last edited:
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Thx @oparra

I appreciate your help!!

B4XFloatTextField1.HintLabelSmallOffsetX = B4XFloatTextField1.TextField.Width - MeasureTextWidth(B4XFloatTextField1.HintText,B4XFloatTextField1.HintFont) - 6

Your uploaded project works fine but in my project that is not working

2021-02-19 (1).png


Spanish:
Publico en el foro de ingles porque creo que habrĆ” mas gente que me pueda ayudar... despues me contestan emn ingles evidentemente y no le digo nada los sufrimientos para traducir y entender. No me trates de usted por favor... soy mayor pero no he llegado a la tercera edad aun. Gracias por tu codigo, de verdad!!
 
Upvote 0

Dadaista

Active Member
Licensed User
Longtime User
Thank You very much @oparra

I really appreciate your help and your time!! :)

Your project uploaded in post #10 works fine but if you change font and font size, it does not work šŸ˜Ÿ
 

Attachments

  • Project3_Modified.zip
    3.4 KB · Views: 140
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Solution B4X:

See:

B4X:
Public Sub SetFloatTextField(FloatTextField As B4XFloatTextField)
    Dim TextFont As B4XFont = xui.CreateFont2(FloatTextField.HintFont, FloatTextField.SmallLabelTextSize)
    Dim TextWidth As Int = MeasureTextWidth(FloatTextField.HintText, TextFont)
    Dim BorderRadius As Int = FloatTextField.TextField.Height / 2
    
    FloatTextField.HintLabelSmallOffsetY = 4
    FloatTextField.HintLabelSmallOffsetX = FloatTextField.TextField.Width - (TextWidth + BorderRadius)
    FloatTextField.Update
    FloatTextField.TextField.SetColorAndBorder(xui.Color_Yellow, DipToCurrent(2), xui.Color_Blue, BorderRadius)
End Sub


1613804949514.png
1613805300030.png
 

Attachments

  • Project4.zip
    15.4 KB · Views: 146
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Just modifying the routine code:


see:
border radius parameter in percent
B4X:
    SetFloatTextField(B4XFloatTextField1, 50)
    SetFloatTextField(B4XFloatTextField2, 50)
    SetFloatTextField(B4XFloatTextField3, 50)

comment and uncomment FloatTextField.HintLabelSmallOffsetY
B4X:
Public Sub SetFloatTextField(FloatTextField As B4XFloatTextField, Radius As Int)
    Dim TextFont As B4XFont = xui.CreateFont2(FloatTextField.HintFont, FloatTextField.SmallLabelTextSize)
    Dim TextWidth As Int = MeasureTextWidth(FloatTextField.HintText, TextFont)
    Dim TextHeight As Int = MeasureTextHeight(FloatTextField.HintText, TextFont)
    Dim BorderRadius As Int = FloatTextField.TextField.Height * (Radius/100)
   
    FloatTextField.HintLabelSmallOffsetY = FloatTextField.TextField.Top - TextHeight + 4
'    FloatTextField.HintLabelSmallOffsetY = FloatTextField.TextField.Height + 4

    FloatTextField.HintLabelSmallOffsetX = FloatTextField.TextField.Width - (TextWidth + BorderRadius)
    FloatTextField.Update
    FloatTextField.TextField.SetColorAndBorder(xui.Color_Yellow, DipToCurrent(2), xui.Color_Blue, BorderRadius)
End Sub

Result:
1613810067376.png
1613810561825.png


1613812110406.png


The rest is up to your imagination.
regards.
 
Last edited:
Upvote 0
Top