Android Question Simple input dialog with coloured text.

Discussion in 'Android Questions' started by RB Smissaert, Jul 29, 2018.

  1. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Currently use this code for a simple inputbox dialog. It uses the Dialogs library, version 4.01:

    Code:
    Sub InputBox(Prompt As String, Title As String, Default As String, Hint As StringAs String
     
    Dim Id As InputDialog
     
    Dim iRet As Int
     
    Id.Hint = Hint
     
    Id.Input = Default
     iRet = 
    Id.Show(Prompt, Title, "OK""""Cancel", General.bmpIcon32)
     
    If iRet = -1 Then
      
    Return Id.Input
     
    Else
      
    Return ""
     
    End If
    End Sub
    This works all fine, but I need to apply some formatting to the prompt text and for that I would like
    to use CSBuilder. The dialogs library doesn't accept Charsequence input though. I want to keep it simple,
    so ideally without adding a lot of extra code. So, something along these lines for a simple Msgbox:

    Code:
    Public Sub ShowMsg(strPrompt As String, _
             strTitle 
    As String, _
             iTitleColour 
    As Int, _
             iPromptColour 
    As Int, _
             csPrompt 
    As CSBuilder, _
             bmpIcon 
    As Bitmap, _
             bCancelable 
    As Boolean)
     
    Dim csTitle As CSBuilder
     
    'so like optional colour argument
     If iTitleColour = 0 Then
      iTitleColour = 
    Colors.RGB(000)
     
    End If
     
    If iPromptColour = 0 Then
      iPromptColour = 
    Colors.RGB(000)
     
    End If
     csTitle.Initialize.Color(iTitleColour).Append(strTitle).Pop
     
    'so csPrompt is like an optional argument, passing Null works fine
     If csPrompt.IsInitialized = False Then
      csPrompt.Initialize.Color(iPromptColour).Append(strPrompt).Pop
     
    End If
     
    'can pass Null for no icon
     Msgbox2Async(csPrompt, csTitle, "OK""""", bmpIcon, bCancelable)
    End Sub
    Any suggestions what the best/simplest way would be?

    RBS
     
  2. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Got this worked out now and seems to work all well.
    Added the ViewsEx library. Added a new layout to my project with a FloatLabeledEditText and
    a simple Label, added directly to the Activity.
    Then added the following code to my main module:

    Code:
    Sub TestInputBox
     
     
    Wait For(InputBox2("""Title Test""Hint test""Default Test", _
            
    Colors.RGB(00160), Colors.RGB(128,128128), 0Null, General.bmpIcon32, True, -1)) _
            Complete (strResult 
    As String)
            
     General.ShowMsg(strResult, 
    "Inputbox test"00Null, General.bmpIcon32, True)
     
    End Sub

    Sub fledtInputbox_TextChanged (Old As String, New As String)
     
     
    If fledtInputbox.IsInitialized Then
      Input_Dialog.GetButton(
    DialogResponse.POSITIVE).Enabled = fledtInputbox.Text.Length > 0
     
    End If

    End Sub

    Sub InputBox2(strPrompt As String, _
         strTitle 
    As String, _
         strHint 
    As String, _
         strDefault 
    As String, _
           iPromptColour 
    As Int, _
         iHintColour 
    As Int, _
         iInputColour 
    As Int, _
           csPrompt 
    As CSBuilder, _
           bmpIcon 
    As Bitmap, _
           bCancelable 
    As Boolean, _
         iPromptHeight 
    As Int) As ResumableSub
         
     
    Dim csPrompt As CSBuilder
     
     
    If iPromptColour = 0 Then
      iPromptColour = 
    Colors.RGB(000)
     
    End If
     
     
    If iHintColour = 0 Then
      iHintColour = 
    Colors.RGB(000)
     
    End If
     
     
    If iInputColour = 0 Then
      iInputColour = 
    Colors.RGB(000)
     
    End If
     
     
    If csPrompt.IsInitialized = False Then
      csPrompt.Initialize.Size(
    16).Color(iPromptColour).Append(strPrompt).PopAll
     
    End If
     
     
    Dim sf As Object = Input_Dialog.ShowAsync(strTitle, "OK""Cancel""", bmpIcon, bCancelable)
     
     
    Wait For (sf) Dialog_Ready(pnl As Panel)
     pnl.LoadLayout(
    "Input_Dialog")
     
     fledtInputbox.Hint = strHint
     fledtInputbox.EditText.TextColor = iInputColour
     fledtInputbox.EditText.HintColor = iHintColour

     
    'as we have the hint, no prompt label may be needed
     If strPrompt.Length > 0 Then
      
    If iPromptHeight = -1 Then
       lblInputBox.Height = sUtils.MeasureMultilineTextHeight(lblInputBox, csPrompt)
      
    Else
       
    'not sure we need this option, MeasureMultilineTextHeight seems to work well
       lblInputBox.Height = iPromptHeight
      
    End If
      General.RunLog(
    "InputBox2, lblInputBox.Height: " & lblInputBox.Height)
      Input_Dialog.SetSize(
    90%x, lblInputBox.Height + 200dip)
      lblInputBox.Text = csPrompt
     
    Else
      Input_Dialog.SetSize(
    90%x200dip)
      lblInputBox.Visible = 
    False
     
    End If
     
     
    If strDefault.Length > 0 Then
      fledtInputbox.Text = strDefault
     
    End If
     
     
    Wait For (sf) Dialog_Result(res As Int)
     
     
    If res = DialogResponse.POSITIVE Then
      
    Return fledtInputbox.text 'OK button
     Else
      
    Return "" 'Cancel button
     End If
     
    End Sub
    Only thing I haven't worked out yet is to alter the formatting of the dialog title text.

    RBS
     
  3. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Any suggestion how I can change the Activity title font?
    This (as suggested in the csBuilder tutorial) didn't do anything:
    Activity.Title = cs

    RBS
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The code from the tutorial works. Please post your code so we can try it...
     
  5. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    OK, thanks, will do.

    RBS
     
  6. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    This is the testing code to see if I can alter the title font of the inputbox dialog:

    Code:
    Sub InputBox2(strPrompt As String, _
         strTitle 
    As String, _
         strHint 
    As String, _
         strDefault 
    As String, _
           iPromptColour 
    As Int, _
         iHintColour 
    As Int, _
         iInputColour 
    As Int, _
           csPrompt 
    As CSBuilder, _
           bmpIcon 
    As Bitmap, _
           bCancelable 
    As Boolean, _
         iPromptHeight 
    As Int) As ResumableSub
         
     
    Dim csPrompt As CSBuilder
     
    Dim csTitle As CSBuilder 'just testing
     If iPromptColour = 0 Then
      iPromptColour = 
    Colors.RGB(000)
     
    End If
     
    If iHintColour = 0 Then
      iHintColour = 
    Colors.RGB(000)
     
    End If
     
    If iInputColour = 0 Then
      iInputColour = 
    Colors.RGB(000)
     
    End If
     
    If csPrompt.IsInitialized = False Then
      csPrompt.Initialize.Size(
    16).Color(iPromptColour).Append(strPrompt).PopAll
     
    End If
     csTitle.Initialize.Size(
    16).Color(Colors.RGB(25500)).Append(strTitle).PopAll 'just testing
     'Dim sf As Object = Input_Dialog.ShowAsync(strTitle, "OK", "Cancel", "", bmpIcon, bCancelable)
     Dim sf As Object = Input_Dialog.ShowAsync(csTitle, "OK""Cancel""", bmpIcon, bCancelable) 'just testing
     Wait For (sf) Dialog_Ready(pnl As Panel)
     pnl.LoadLayout(
    "Input_Dialog")
     fledtInputbox.Hint = strHint
     fledtInputbox.EditText.TextColor = iInputColour
     fledtInputbox.EditText.HintColor = iHintColour
     
    Activity.TitleColor = Colors.RGB(25500'just testing
     'as we have the hint, no prompt label may be needed
     If strPrompt.Length > 0 Then
      
    If iPromptHeight = -1 Then
       lblInputBox.Height = sUtils.MeasureMultilineTextHeight(lblInputBox, csPrompt) + 
    20dip
      
    Else
       
    'not sure we need this option, MeasureMultilineTextHeight seems to work well
       lblInputBox.Height = iPromptHeight
      
    End If
      General.RunLog(
    "InputBox2, lblInputBox.Height: " & lblInputBox.Height)
      Input_Dialog.SetSize(
    96%x, lblInputBox.Height  + 200dip)
      lblInputBox.Text = csPrompt
     
    Else
      Input_Dialog.SetSize(
    96%x200dip)
      lblInputBox.Visible = 
    False
     
    End If
     
    If strDefault.Length > 0 Then
      fledtInputbox.Text = strDefault
     
    End If
     
    Wait For (sf) Dialog_Result(res As Int)
     
    If res = DialogResponse.POSITIVE Then
      
    Return fledtInputbox.text 'OK button
     Else
      
    Return "" 'Cancel button
     End If
    End Sub
    This doesn't work as the title font is the same, so not red.
    So, leaving out all the lines marked as 'just testing (and uncommenting one line) makes no difference.
    Probably overlooking something really simple, but can't see it yet.

    RBS
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    I don't understand anything. Where is the Activity.Title = cs line?

    How is it related to this dialog?
     
  8. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Is this line meant to be setting the title colour?

    Code:
    Dim sf As Object = Input_Dialog.ShowAsync(csTitle, "OK""Cancel""", bmpIcon, bCancelable) 'just testing
    At work now, but will add Activity.Title = cs and see if that does the title formatting.

    RBS
     
  9. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Think have solved this now. Indeed Activity has nil to do with this. Not sure now how I got that in.
    Probably because nil was working.

    This works nicely:

    Code:
    Sub InputBox2(strPrompt As String, _
         strTitle 
    As String, _
         strHint 
    As String, _
         strDefault 
    As String, _
         iTitleFontSize 
    As Int, _
         iTitleFontColour 
    As Int, _
           iPromptColour 
    As Int, _
         iHintColour 
    As Int, _
         iInputColour 
    As Int, _
           csPrompt 
    As CSBuilder, _
           bmpIcon 
    As Bitmap, _
           bCancelable 
    As Boolean, _
         iPromptHeight 
    As Int) As ResumableSub
         
     
    Dim csPrompt As CSBuilder
     
    Dim csTitle As CSBuilder
     
    Dim sf As JavaObject
     
    If iTitleFontColour = 0 Then
      iTitleFontColour = 
    Colors.RGB(000)
     
    End If
     
    If iPromptColour = 0 Then
      iPromptColour = 
    Colors.RGB(000)
     
    End If
     
    If iHintColour = 0 Then
      iHintColour = 
    Colors.RGB(000)
     
    End If
     
    If iInputColour = 0 Then
      iInputColour = 
    Colors.RGB(000)
     
    End If
     
    If csPrompt.IsInitialized = False Then
      csPrompt.Initialize.Size(
    16).Color(iPromptColour).Append(strPrompt).PopAll
     
    End If
     csTitle.Initialize.Typeface(
    Typeface.DEFAULT).Size(iTitleFontSize).Color(iTitleFontColour).Append(strTitle).PopAll
     sf = Input_Dialog.ShowAsync(csTitle, 
    "OK""Cancel""", bmpIcon, bCancelable)
     sf.RunMethod(
    "setTitle"Array(csTitle))
     
    Wait For (sf) Dialog_Ready(pnl As Panel)
     pnl.LoadLayout(
    "Input_Dialog")
     fledtInputbox.Hint = strHint
     fledtInputbox.EditText.TextColor = iInputColour
     fledtInputbox.EditText.HintColor = iHintColour
     
    'as we have the hint, no prompt label may be needed
     If strPrompt.Length > 0 Then
      
    If iPromptHeight = -1 Then
       lblInputBox.Height = sUtils.MeasureMultilineTextHeight(lblInputBox, csPrompt) + 
    20dip
      
    Else
       
    'not sure we need this option, MeasureMultilineTextHeight seems to work well
       lblInputBox.Height = iPromptHeight
      
    End If
      General.RunLog(
    "InputBox2, lblInputBox.Height: " & lblInputBox.Height)
      Input_Dialog.SetSize(
    96%x, lblInputBox.Height  + 200dip)
      lblInputBox.Text = csPrompt
     
    Else
      Input_Dialog.SetSize(
    96%x200dip)
      lblInputBox.Visible = 
    False
     
    End If
     
    If strDefault.Length > 0 Then
      fledtInputbox.Text = strDefault
     
    End If
     
    Wait For (sf) Dialog_Result(res As Int)
     
    If res = DialogResponse.POSITIVE Then
      
    Return fledtInputbox.text 'OK button
     Else
      
    Return "" 'Cancel button
     End If
    End Sub
    This is the line that was needed:
    sf.RunMethod("setTitle", Array(csTitle))


    RBS
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice