Android Code Snippet [B4X] XUI Views - Time Dialog

Discussion in 'Code Snippets' started by aeric, Apr 19, 2019.

  1. aeric

    aeric Active Member Licensed User

    I want a better dialog for time input so I created this time (picker) dialog using B4X XUI Views and IME library. I think the code has too many lines. Hope someone can simplify or improve it.

    Note: I haven't tested but I think this code can also work for B4i and B4J.

    Update: I have tested with B4A, B4i and B4J. For B4i and B4J I am not using IME library.

    Bug: In B4J, even though I have set the Keyboard Type to Numbers for B4XFloatTextField, I still can enter alphabet which cause the app crashed.

    image.png

    Here is the code for B4A:
    Code:
    Sub Process_Globals
        
    Private xui As XUI
    End Sub

    Sub Globals
        
    Dim IME As IME
        
    Private dialog As B4XDialog
        
    Private lblTime As Label
        
    Private ftfHour As B4XFloatTextField
        
    Private ftfMinute As B4XFloatTextField
        
    Private cbAMPM As B4XComboBox
        
    Dim HH As Int = 12
        
    Dim MM As Int = 0
        
    Dim TT As Int = 1
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("1")
        dialog.Initialize(
    Activity)
        dialog.Title = 
    "B4AViewTimeDialog"
    End Sub

    Sub Activity_Resume
     
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        dialog.Close(xui.DialogResponse_Cancel)
    End Sub

    Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
        If KeyCode = KeyCodes.KEYCODE_BACK Then
            
    If dialog.Close(xui.DialogResponse_Cancel) Then Return True
        
    End If
        
    Return False
    End Sub

    Sub lblTime_Click
        
    Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(
    000100%x-40dip150dip)
        p.LoadLayout(
    "CustomDialog")
        
    IME.Initialize("IME")
        
    IME.SetLengthFilter(ftfHour.TextField, 2)
        
    IME.SetLengthFilter(ftfMinute.TextField, 2)
        ftfHour.TextField.EditTextHint = 
    "12"
        ftfHour.TextField.SetTextAlignment(
    "CENTER""CENTER")
        ftfMinute.TextField.EditTextHint = 
    "00"
        ftfMinute.TextField.SetTextAlignment(
    "CENTER""CENTER")
        cbAMPM.SetItems(
    Array As String("AM""PM"))
        cbAMPM.SelectedIndex = TT
        dialog.PutAtTop = 
    True 'put the dialog at the top of the screen
        dialog.BackgroundColor = Colors.ARGB(200125125125)
        dialog.ButtonsColor = 
    Colors.RGB(125125125)
        
    If HH < 10 Then ftfHour.Text = "0" & HH Else ftfHour.Text = HH
        
    If MM < 10 Then ftfMinute.Text = "0" & MM Else ftfMinute.Text = MM
        
    Wait For (dialog.ShowCustom(p, "OK""""CANCEL")) Complete (Result As Int)
        
    If Result = xui.DialogResponse_Positive Then
            
    If ftfHour.Text = "" Or ftfHour.Text = "0" Or ftfHour.Text = "00" Then HH = 12 Else HH = ftfHour.Text
            
    If ftfMinute.Text = "" Then MM = 0 Else MM = ftfMinute.Text
            
    If HH > 12 Then HH = 12
            
    If MM > 59 Then MM = 0
            
    If HH < 10 Then ftfHour.Text = "0" & HH Else ftfHour.Text = HH
            
    If MM < 10 Then ftfMinute.Text = "0" & MM Else ftfMinute.Text = MM
            TT = cbAMPM.SelectedIndex
            
    Dim strValue As String = ftfHour.Text & ":" & ftfMinute.Text & " " & cbAMPM.GetItem(cbAMPM.SelectedIndex)
            
    'dialog.Show(strValue, "OK", "", "")
            lblTime.Text = strValue
        
    End If
    End Sub
     

    Attached Files:

    Last edited: Apr 19, 2019
  2. aeric

    aeric Active Member Licensed User

    Post #1 updated with B4A, B4i and B4J.
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    See how the templates are implemented (unzip XUI Views.b4xlib). You can convert this code to a template class.
     
    aeric likes this.
  4. aeric

    aeric Active Member Licensed User

    Thanks Erel. I will try as your suggestion. Enjoy your holiday!
     
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