Android Question how to make chats well centered

Makumbi

Well-Known Member
Licensed User
Screenshot_1586706862.png


B4X:
#Region  Project Attributes
    #ApplicationLabel: Chat Example
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private xui As XUI
    Private clvMessages As CustomListView
    Private lblName As Label
    Private ldates As Label
    Private lblMessage As Label
    Private messageSize As Int = 85%x
    Private margin As Int = 2%x
    Private internalYMargin As Int = 2%y
    Private pWhite As Panel
    Private tracker As Label
    Private leftColor As Int = Colors.White
    Private rightColor As Int = Colors.RGB(255,243,209)
    Private border As Boolean = True
    Private borderColor As Int = Colors.Black
    Private borderWidth As Int = 2dip
    'Private TextField As B4XFloatTextField
    Private ComboBox1 As B4XComboBox
'    Private edtMessage As EditText
    Private edtMessage As B4XFloatTextField
    Private InChatClient, OutChatClient As String
    Private ClientID As Int   '???? Needed Maybe  'ignore
    Private ime As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    'this example is an adaptation of a project / example by @Douglas Farias  Dec 2017
    'https://www.b4x.com/android/forum/threads/clv-simple-chat-example-adjustable-text-size.87063/#content
    '-------------------------------------------------------------------------------------------------------------------
    
    Activity.LoadLayout("main")
    ime.Initialize("ime")
    'pWhitet.IsInitialized
'    ime.AddHeightChangedEvent
'    pWhite.Width = 100%x - messageSize - margin
    ScrollToLastItem

    clvMessages.AsView.Color = xui.Color_Transparent   ' clv background color = activity color
    
    ComboBox1.SetItems(Array As String( " ", "Winston Churchill", "Homer Simpson", "Kylo Ren")) 'first item blank ? ToBeInvestigated
    ComboBox1.cmbBox.DropdownBackgroundColor = Colors.White
    ComboBox1.cmbBox.DropdownTextColor = Colors.Black
    ComboBox1.cmbBox.TextColor = Colors.Black
    
    
    Log(ComboBox1.SelectedIndex)
    
    OutChatClient = "Makumbi"
    
    
    '@@@ from original example ...
    'HERE A SIMPLE EXAMPLE OF HOW ADD A MESSAGE
    'Add_Conversation(True,"Douglas Farias","Hi all! Here is a simple message example")
    
End Sub

Sub LoadMessages(client As String)
    
    Dim ticks As Long                'ignore
    Dim message, IOflag As String
    
    Dim rs As ResultSet = Starter.oSQL.ExecQuery2("SELECT Timestamp, Message, IOFlag FROM tbl_Messages WHERE ChatClientName = ? ORDER BY Timestamp ASC", Array As String(client))
 
    ' Log(rs.RowCount)
 
    Do While rs.NextRow
        ticks = rs.GetLong("Timestamp")
        
        Log(ticks)
        message = rs.GetString("Message")
        Log(message)
        
        IOflag = rs.GetString("IOflag")

Log(IOflag)

        'load the messages Left or right dependant on stored IOflag (In or Out Message)
        If IOflag = "IN" Then
            clvMessages.Add(Add_Conversation(True,InChatClient,message,ticks), 0)
        Else
            clvMessages.Add(Add_Conversation(False,OutChatClient, message,ticks), 0)
        End If
    Loop
    
    Sleep(300)
    If clvMessages.Size >0 Then clvMessages.ScrollToItem(clvMessages.Size - 1)
    
End Sub

Sub btnSendReceive_Click
    
    'Dim lt As Long
    'DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a" ' "1961-08-29"
    'Dim datestring As String = DateTime.Date(getdate( DateTime.Now))
    'lt = DateTime.DateParse(datestring)
    'DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a"
    'Log(DateTime.Date(lt))
    Dim holds As Long = DateTime.Now
    Dim btn As Button = Sender
    
    If btn .Tag = "Send" Then
        'save NEW message to the db  (as OUTGOING message) and add to CLV
        
        'DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a" ' "1961-08-29"
        Starter.oSQL.ExecNonQuery2("INSERT INTO tbl_Messages VALUES( NULL, ?, ?, ?, ? )", Array As Object((holds), InChatClient, edtMessage.Text, "OUT"))
        clvMessages.Add(Add_Conversation(False,OutChatClient, edtMessage.text,holds), 0)
        ScrollToLastItem
    Else  'Receive
        
        'save NEW message to the db  (as INCOMINGcomming message) and add to CLV
        Starter.oSQL.ExecNonQuery2("INSERT INTO tbl_Messages VALUES( NULL, ?, ?, ?, ? )", Array As Object((holds), InChatClient, edtMessage.Text, "IN"))
        clvMessages.Add( Add_Conversation(True,InChatClient, edtMessage.text,holds),0)
    End If
    
    edtMessage.text = ""
    
    Sleep(300)
    If clvMessages.Size >0 Then clvMessages.ScrollToItem(clvMessages.Size - 1)

End Sub

Sub ComboBox1_SelectedIndexChanged (Index As Int)
        
    'InChatClient = ComboBox1.SelectedItem
    
    clvMessages.Clear
    
    'Get all chats with this client ...
    LoadMessages(InChatClient)
    
End Sub


Sub  getdate(gt As String) As String
    Dim lt As Long
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a" ' "1961-08-29"
    Dim datestring As String = DateTime.Date(( gt))
    lt = DateTime.DateParse(datestring)
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a"
    Log(DateTime.Date(lt))
    'gt=DateTime.Date(lt)
    Return DateTime.Date(lt)
    
End Sub


Sub Add_Conversation (leftChat As Boolean, name As String, message As String, dates As String)  As Panel

    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, margin, 0, messageSize, 5%y)
    p.LoadLayout("clvItems")
        
        Log(dates)
    'HERE WE ADD THE MESSAGE AND NAME TO LBNAME AND LBMESSAGE
    lblName.Text = name
    lblMessage.Text = message
    ldates.Text= getdate(dates)
    tracker.Text=dates
    tracker.Visible=False
    'Dim holdt As Long
    'holdt= DateTime.DateParse(getdate(dates))
    'Log(holdt)
    Log(tracker.Text)
    Log(ldates.Text)
    'GET IF IS LEFT OR RIGHT CHAT
    If leftChat Then
        pWhite.Left = margin
        lblName.Gravity = Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
        lblMessage.Gravity = Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
        ldates.Gravity = Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)

    Else
        ldates.Width= 600%x
        ldates.Left = 80%x-180%x
        pWhite.Left = 100%x - messageSize - margin
        lblName.Gravity = Bit.Or(Gravity.RIGHT,Gravity.TOP)
        lblMessage.Gravity = Bit.Or(Gravity.RIGHT,Gravity.TOP)
        ldates.Gravity = Bit.Or(Gravity.LEFT,Gravity.TOP)
    End If
        
    'HERE WE GET THE MESSAGE AND NAME CHAT
    Private su As StringUtils
    ldates.Left = 60%x-50%x
    ldates.Width= 500%x
    lblName.Top = 0 + internalYMargin
    lblName.Height =su.MeasureMultilineTextHeight(lblName,lblName.Text)
    lblMessage.Height =su.MeasureMultilineTextHeight(lblMessage,lblMessage.Text)
    ldates.Height =su.MeasureMultilineTextHeight(ldates,ldates.Text)
    lblMessage.Top = lblName.Top + lblName.Height
    'ldates.Top = ldates.Top + ldates.Height
    'lblMessage.Width= 230%x
                
    'LATER GET THE CORRECT SIZE NEEDED WE APPLY THIS TO PANEL
    p.Height = lblName.Height + lblMessage.Height + (internalYMargin * 2)
    pWhite.Height = lblName.Height + lblMessage.Height + (internalYMargin * 2)
    'pWhite.Height = ldates.Height + ldates.Height + (internalYMargin * 2)
    
    
    
    'HERE WE SET THE COLOR AND BORDER TO THE MESSAGE PANEL
    If border Then
        If leftChat Then
            Private cd As ColorDrawable
            cd.Initialize2(leftColor,5,borderWidth,borderColor)
            pWhite.Background = cd
            'pWhite.Color = cd
        Else
            Private cd As ColorDrawable
            cd.Initialize2(rightColor,5,borderWidth,borderColor)
            pWhite.Background = cd
            'pWhite.Color = cd
            
        End If
    Else
        If leftChat Then
            Private cd As ColorDrawable
            cd.Initialize2(leftColor,5,0,borderColor)
            pWhite.Background = cd
            '    pWhite.Color = cd
        Else
            Private cd As ColorDrawable
            cd.Initialize2(rightColor,5,0,borderColor)
            'pWhite.Color = cd
            pWhite.Background = cd
        End If
    End If
    
    p.Visible = True
    
    Return p
        
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub ScrollToLastItem
    Sleep(50)
    If clvMessages.Size > 0 Then
        If clvMessages.sv.ScrollViewContentHeight > clvMessages.sv.Height Then
            clvMessages.ScrollToItem(clvMessages.Size - 1)
        End If
    End If
End Sub

Sub clvMessages_ItemLongClick (Index As Int, Value As Object)
    Dim value1, value2,value3 As String
    Dim holdk As String
    'we need to get the values from the views
    Dim content As B4XView = clvMessages.GetPanel(Index).GetView(0)
    value1 = content.GetView(0).Text
    value2 = content.GetView(1).Text
    value3 = content.GetView(2).Text
    holdk = content.GetView(3).Text

    Log($"Value1: ${value1}, Value2: ${value2},Value3: ${value3}"$)
    Log(holdk)
    
    Dim hold As Long
    'DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a" ' "1961-08-29"
    hold=holdk
    
        
    
    Log(holdk)
    
    Log(hold)
    
    Log($"Value1: ${value1}, Value2: ${value2},Value3: ${value3}"$)
    Log("ItemLongClick " & Index)
    Log(Value)
    Msgbox2Async("Do you Want to delete this Message?", "Delete Conversation", "Yes", "Cancel", "No", Null, True)
    Wait For Msgbox_Result (Result As Int)
    
    Log(Result)
    If Result = DialogResponse.POSITIVE Then
        'Dim rsT As ResultSet
        'Starter.oSQL.ExecQuery2("DELETE  FROM tbl_Messages WHERE ChatClientName = ?", Array As String(value2))
        Starter.OSQL.ExecNonQuery2("DELETE FROM tbl_Messages WHERE Timestamp = ?", Array As String (hold))  'deletes from DB
    
        clvMessages.RemoveAt(Index)
        Return
    Else
            
    End If
End Sub
 

Attachments

  • Backup ChatExample 2020-04-12 18.53.zip
    13 KB · Views: 116
Top