Android Example Clv Simple Chat Example (adjustable text size)

Douglas Farias

Expert
Licensed User
Hi All.
Here is a very very simple example of how make a chat using clv.
the text and name is ajustable automatic, you can receive any text and the code will ajust using the correct size.

The example is on portuguese but is very very easy to understand, around 30 lines of code.


On this example we have the user name, and the message, to simulate a chat, but you can easy add more views.
Screenshot_20171209-170457.png


on the example i m reading the txt files, the txt files are added on dir assets folder.


thx
 

Attachments

Douglas Farias

Expert
Licensed User
Here is another simple chat example

B4X:
#Region  Project Attributes
    #ApplicationLabel: Exemplo CLV
    #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 clvMensagens As CustomListView
    Private lbNome As Label
    Private lbMensagem 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 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 = 1dip
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
  
  
  
  
    'HERE IS A EXAMPLE OF HOW ADD CHAT MESSAGE WITH FOR
    For I = 0 To 4

        If i = 0 Then
            Add_Conversation(True,File.ReadString(File.DirAssets,"nome1.txt"),File.ReadString(File.DirAssets,"mensagem1.txt"))
        else if i = 1 Then
            Add_Conversation(False,File.ReadString(File.DirAssets,"nome2.txt"),File.ReadString(File.DirAssets,"mensagem2.txt"))
        else if i = 2 Then
            Add_Conversation(True,File.ReadString(File.DirAssets,"nome3.txt"),File.ReadString(File.DirAssets,"mensagem3.txt"))
        else if i = 3 Then
            Add_Conversation(False,File.ReadString(File.DirAssets,"nome1.txt"),File.ReadString(File.DirAssets,"mensagem1.txt"))
        else if i = 4 Then
            Add_Conversation(True,File.ReadString(File.DirAssets,"nome1.txt"),File.ReadString(File.DirAssets,"mensagem1.txt"))
        End If

    Next
  
  
  
    '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 Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub






Sub Add_Conversation (leftChat As Boolean, name As String, message As String)
  
    Private p As Panel
    p.Initialize("p")
    p.Visible = False
    Activity.AddView(p,margin,0,messageSize,5%Y)
    p.LoadLayout("dentroClv")
    p.RemoveView
      
    'HERE WE ADD THE MESSAGE AND NAME TO LBNAME AND LBMESSAGE
    lbNome.Text = name
    lbMensagem.Text = message
      
      
      

    'GET IF IS LEFT OR RIGHT CHAT
    If leftChat Then
        pWhite.Left = margin
        lbNome.Gravity = Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
        lbMensagem.Gravity = Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
    Else
        pWhite.Left = 100%x - messageSize - margin
        lbNome.Gravity = Bit.Or(Gravity.RIGHT,Gravity.TOP)
        lbMensagem.Gravity = Bit.Or(Gravity.RIGHT,Gravity.TOP)
    End If
      
      
      
    'HERE WE GET THE MESSAGE AND NAME CHAT
    Private su As StringUtils
    lbNome.Top = 0 + internalYMargin
    lbNome.Height =su.MeasureMultilineTextHeight(lbNome,lbNome.Text)
    lbMensagem.Height =su.MeasureMultilineTextHeight(lbMensagem,lbMensagem.Text)
    lbMensagem.Top = lbNome.Top + lbNome.Height
      
      
    'LATER GET THE CORRECT SIZE NEEDED WE APPLY THIS TO PANEL
    p.Height = lbNome.Height + lbMensagem.Height + (internalYMargin * 2)
    pWhite.Height = lbNome.Height + lbMensagem.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
        Else
            Private cd As ColorDrawable
            cd.Initialize2(rightColor,5,borderWidth,borderColor)
            pWhite.Background = cd
        End If
    Else
        If leftChat Then
            Private cd As ColorDrawable
            cd.Initialize2(leftColor,5,0,borderColor)
            pWhite.Background = cd
        Else
            Private cd As ColorDrawable
            cd.Initialize2(rightColor,5,0,borderColor)
            pWhite.Background = cd
        End If
    End If
  
  
  
  
      
      
    p.Visible = True
    'HERE WE ADD THE CONVERSATION PANEL WITH NAME AND MESSAGE TO CLV
    clvMensagens.Add(p,p.Height,0)
      
  

  
End Sub
Screenshot_20171213-181615.png Screenshot_20171213-181620.png


with this example you can select colors, sizes, margin etc...

to add a new conversation chat you can use this sub
B4X:
 'HERE A SIMPLE EXAMPLE OF HOW ADD A MESSAGE
    Add_Conversation(True,"Douglas Farias","Hi all! Here is a simple message example")
Sub Add_Conversation (leftChat As Boolean, name As String, message As String)

thx
 

Attachments

Beja

Expert
Licensed User
Hi Douglas,
This is not a real chat between 2 Android devices, right? please correct me if I am wrong.. I think it may be a chat simulation.
 

Douglas Farias

Expert
Licensed User
Hi Douglas,
This is not a real chat between 2 Android devices, right? please correct me if I am wrong.. I think it may be a chat simulation.
Sorry to the delay...
is exactly what DonManfred said.

just a simple layout demonstration of how to make a chat with clv.

thx
 

Carlos marin

Active Member
Licensed User
Hi Douglas
hello how do I do it so that the scroll is always down, and tried with
clvM.ScrollToItem and clvM.JumpToItem,
But it's not working
 

Nkalampika

Active Member
Licensed User
hello try it ( its work for me )
CallSubDelayed(Me,"scroll")

Sub scroll
clvMensagens.JumpToItem(clvMensagens.GetSize)

End Sub
 
Top