B4J Question Adjust hint and text in FloatTextField

jroriz

Active Member
Licensed User
I would like to adjust the position of the hint and the text being typed in the FloatTextField.
Notice that I'd like to move the hint a little bit too.
Could anyone help?

from:
from.png

to:
to.png
 

mangojack

Well-Known Member
Licensed User
You can align the text using ..
B4X:
    Dim jo1 = FloatTextField1.TextField     As JavaObject
    jo1.RunMethod("setAlignment", Array As Object("CENTER")) 'CENTER_RIGHT



As far as the hint text .. I tried to adapt some B4A code by @Erel Here ...
B4X:
SetHintAlignmnetCenter(FloatTextField1)

Sub SetHintAlignmnetCenter(ft As B4XFloatTextField)
   
    Dim HintWidth As Int = 200dip
    Dim HintHeight As Int = 60dip
    Dim HintImageView As B4XView = ft.mBase.GetView(1)   
   
    HintImageView.RemoveViewFromParent
    Dim pnl As B4XView = xui.CreatePanel("")
    'ft.mBase.AddView(pnl, ft.mBase.Width - HintImageView.Width - 10dip, 0, HintImageView.Width + 10dip, ft.mBase.Height) 'original snippet
    ft.mBase.AddView(pnl, ft.mBase.Width/2 - HintWidth/2, 0, HintWidth, HintHeight) 'size accordingly
    pnl.AddView(HintImageView, 6dip, HintImageView.Top, HintImageView.Width, HintImageView.Height)

End Sub

It would appear the HintText is an ImageView and unfortunately ..
1. you only seem to get the height and width after text has been entered (thus the manual sizing above)
2. you end up with a panel holding the hint text (thus you initially have to click the the TextField on a non hint area.)

maybe take it for a spin and make your own decision.

@Erel will probably fly by soon with a more correct /succinct code anyway.
 
Upvote 0

jroriz

Active Member
Licensed User
You can align the text using ..
B4X:
    Dim jo1 = FloatTextField1.TextField     As JavaObject
    jo1.RunMethod("setAlignment", Array As Object("CENTER")) 'CENTER_RIGHT



As far as the hint text .. I tried to adapt some B4A code by @Erel Here ...
B4X:
SetHintAlignmnetCenter(FloatTextField1)

Sub SetHintAlignmnetCenter(ft As B4XFloatTextField)
  
    Dim HintWidth As Int = 200dip
    Dim HintHeight As Int = 60dip
    Dim HintImageView As B4XView = ft.mBase.GetView(1)  
  
    HintImageView.RemoveViewFromParent
    Dim pnl As B4XView = xui.CreatePanel("")
    'ft.mBase.AddView(pnl, ft.mBase.Width - HintImageView.Width - 10dip, 0, HintImageView.Width + 10dip, ft.mBase.Height) 'original snippet
    ft.mBase.AddView(pnl, ft.mBase.Width/2 - HintWidth/2, 0, HintWidth, HintHeight) 'size accordingly
    pnl.AddView(HintImageView, 6dip, HintImageView.Top, HintImageView.Width, HintImageView.Height)

End Sub

It would appear the HintText is an ImageView and unfortunately ..
1. you only seem to get the height and width after text has been entered (thus the manual sizing above)
2. you end up with a panel holding the hint text (thus you initially have to click the the TextField on a non hint area.)

maybe take it for a spin and make your own decision.

@Erel will probably fly by soon with a more correct /succinct code anyway.
Thanks for your answer.
In fact my main goal is to work with the textfield top.
I tried:
B4X:
B4XFloatTextField1.TextField.Top = B4XFloatTextField1.TextField.Top + 5
but the result was not as expected...
Capturar1.PNG
Capturar2.PNG
 
Upvote 0

William Lancee

Active Member
Licensed User
I would not recommend doing this, because it breaks the benefits of encapsulation: if the customview is changed in the future your program may have a difficult_to find_bug.
Also the B4XFloatTextField.B4XLib can be looked at (unzip it) and modified to your needs and added to your project and renamed.
Nevertheless, I have found the following approach helps me gain insight.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show

    B4XFloatTextField1.Text = "Some Text"
    Dim XTextField As B4XView = B4XFloatTextField1.TextField

    Dim XTextFieldParent As B4XView = XTextField.Parent
    analyzeCustomView(XTextFieldParent)
    'Based on the results, the relevant nodes are the visible panel, the TextField, an Image Field, and 2 labels

    Dim XVisiblePanel As B4XView = XTextFieldParent.GetView(0)
    XVisiblePanel.Color = xui.Color_Red
    XVisiblePanel.Height = 2 * XVisiblePanel.Height

    Dim XClose As B4XView = XTextFieldParent.GetView(2)
    XClose.TextColor = xui.Color_Yellow

    Dim XOK As B4XView = XTextFieldParent.GetView(3)
    XOK.TextColor = xui.Color_Green

    'the Hint ImageView can be manipulated, but only after sleep(0)!  Of course when the text is deleted the Hint reverts back to the original.
    'You have to put it in the 'B4XFloatTextField1_Changed' event

'Sub B4XFloatTextField1_TextChanged(old As String, new As String)
'     Dim XHint As ImageView = B4XFloatTextField1.TextField.parent.GetView(1)
'     If new.Length>0 Then XHint.SetLayoutAnimated(0, 8dip, 8dip, -1, -1)
'End Sub


    Sleep(0)
    Dim XHint As ImageView = XTextFieldParent.getView(1)
    XHint.SetLayoutAnimated(0, 10dip, 10dip, 40dip, 20dip)
End Sub

B4X:
Sub analyzeCustomView(XParentContainer As B4XView)
    For i = 0 To XParentContainer.NumberOfViews - 1
        Try
            Dim B4X As B4XView = XParentContainer.GetView(i)
            Try
                Log(i & TAB & GetType(B4X) & TAB & QUOTE & B4X.Text & QUOTE)
            Catch
                Try
                    Log(i & TAB & GetType(B4X) & TAB & B4X.Left & TAB & B4X.Top & TAB & B4X.Width & TAB & B4X.Height)
                Catch
                    Log(i & "*" & TAB & GetType(B4X))
                End Try
            End Try
            analyzeCustomView(B4X)
        Catch
        End Try    'ignore
    Next
End Sub
 
Last edited:
Upvote 0
Top