B4J Question Multiline text input - how do you do this.

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

I wish to enter multi-line text in a text box which will capture the carriage returns and line feeds into a UTF8 string. I'm sure I have done this before, but for the life of me I can't figure it out.

Can anyone help here?

Rob
 

ddk1

Member
Licensed User
This is what I use to allow multiline user input. Customised version of some code I found on forum some years ago.

Proc:
'
' Because it uses async calls, it is a resumable sub, so can't simply return a value.
' Has to go in an activity module, so can't go in Lib module (which is static)
' Calling syntax: Wait For(libloc_MultilineInputDialog(pstrText, pblnWrap)) Complete (strInput As String)
' Returns: text if accepted, empty text if user cleared (or entered no text), QUOTE if user pressed back key (so do nothing)
' Uses StateManager to save/reinstate what user typed so far in case app gets swapped out of foreground
'
Sub libloc_MultilineInputDialog(pstrText As String, pblnWrap As Boolean) As ResumableSub
    
    Dim cd As CustomLayoutDialog    'requires lib Dialogs   
    cd.ShowAsync("", "Ok", "", "Clear", Null, True)
    cd.SetSize(92%x, 50%y) 'request maximum size
    Wait For Dialog_Ready (DialogPanel As Panel)
    Dim txt As EditText
    txt.Initialize("libloc_MultilineInputDialog")
    txt.SingleLine = False
    txt.Wrap = pblnWrap
    If pstrText = "" Then
        txt.Text = StateManager.GetSetting("Txt")
    Else
        txt.Text = pstrText
    End If
    txt.Color = Colors.RGB(224, 255, 255)     'LightCyan
    txt.TextColor = Colors.Black
    txt.InputType = Bit.Or(txt.InputType, 524288)    'disable spellcheck
    txt.Gravity = Gravity.TOP + Gravity.LEFT
    If txt.Text.Length > 0 Then
        txt.SelectionStart = 1    'if don't set to 1 first it doesn't go to 0
    End If
    txt.SelectionStart = 0
    DialogPanel.AddView(txt, 0, 0, 86%x, 40%y)
    Dim ime As IME        'requires lib IME
    ime.Initialize("")
    Sleep(100)            'seem to need longish sleep here to get kbd to show depending on what was done before calling this
    ime.ShowKeyboard(txt)
    Wait For Dialog_Result(Result As Int)
    Sleep(100)            'in some scenarios the kbd
    ime.HideKeyboard    '  pops up again after
    StateManager.SetSetting("Txt", "")
    StateManager.SaveSettings
    If Result = DialogResponse.POSITIVE Then
        Return txt.Text.Trim
    else If Result = DialogResponse.NEGATIVE Then
        Return ""
    Else
        Return QUOTE    'flag to do nothing
    End If

End Sub
Sub libloc_MultilineInputDialog_TextChanged (Old As String, New As String)
    StateManager.SetSetting("Txt", New)
    StateManager.SaveSettings
End Sub
 
Upvote 0
Top