Android Question Problem with MeasureMultilineTextHeight

Luiz Fernando Orlandini

Active Member
Licensed User
Hey All.

I'm trying to simulate a kind of chat in my APP, but when I test my APP in different devices (specially with bigger screens), I get different spaces between my labels. They got a higher height than should get.

Please... What am I doing wrong?

Look that txtBodyMessage was created by Designer

B4X:
Private Sub LoadMessages(Messages As MessageJson)
    If (Messages.statusId > 0) Then
        Dim l As List
        l.Initialize
      
        Dim su As StringUtils
        For i = 0 To Messages.MessageList.Size - 1
            Dim m As MessageType
            m = Messages.MessageList.Get(i)
            txtBodyMessage.Text = m.BodyMessage.Trim

            Dim H As Long = su.MeasureMultilineTextHeight(txtBodyMessage, m.BodyMessage.Trim) * 1dip
            Dim HLine As Long
          
            If (H + 55 < 135) Then
                HLine = 135
            Else
                HLine = H + 55
            End If
          
            HistMessageList.Add(CreateListItem(m, HistMessageList.AsView.Width, H), HLine, m)
        Next
      
        HistMessageList.JumpToItem(HistMessageList.GetSize)
    End If
End Sub




Private Sub CreateListItem(Msg As MessageType, width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Transparent
  
    Subject = Msg.Subject
  
    Dim photo As ImageView
    photo.Initialize("ImageView")
    photo.Gravity = Gravity.FILL
    If (Msg.FromUser.photo.Length > 0) Then
        Dim u As Utils
        u.Initialize
        photo.Bitmap = u.loadPhoto(Msg.FromUser.photo)
    End If

    Dim lblMessage As Label
    lblMessage.Initialize("lblMessage")
    lblMessage.Text = Msg.BodyMessage.Trim
    lblMessage.TextSize = 16
    lblMessage.TextColor = Colors.White
    lblMessage.Gravity = Gravity.CENTER_HORIZONTAL
      
    Dim lblSentDateTime As Label
    lblSentDateTime.Initialize("lblSentDateTime")
    lblSentDateTime.Text = Msg.SentDateTime
    lblSentDateTime.TextSize = 10
    lblSentDateTime.TextColor = Colors.White

    Dim widthFont As Int = 0.625 * Activity.width

    Dim Left As Long = width - 115dip - widthFont
    If Left < 0 Then
        Left = 5
    End If
  
    If (Msg.FromUser.UserId = Main.Responsible.responsibleId) Then
        Dim d As ColorDrawable
        d.Initialize(Colors.RGB(150,150,150), 10)
        lblMessage.Background = d
        lblMessage.Color = Colors.RGB(150,150,150)
         lblSentDateTime.Gravity = Gravity.RIGHT
         p.AddView(photo, width - 90dip, 10dip, 80dip, 80dip) 'view #0
         p.AddView(lblMessage, Left, 20dip, widthFont + 20dip, Height) 'view #0
         p.AddView(lblSentDateTime, Left, Height+25dip, widthFont + 20dip, 20dip) 'view #0
        ReplyToUserId = Msg.ToUser.UserId
           
     Else 'Align on the left position
         ReplyToUserId = Msg.FromUser.UserId
        Dim d As ColorDrawable
        d.Initialize(Colors.RGB(255, 164, 49), 10)
        lblMessage.Background = d
         lblMessage.Color = Colors.RGB(255, 164, 49)
         lblSentDateTime.Gravity = Gravity.Left
         'Objeto, Left, Top, Width, Heigth
         p.AddView(photo, 10dip, 10dip,  80dip, 80dip) 'view #0
         p.AddView(lblMessage, 95dip, 20dip, widthFont + 20dip, Height) 'view #0
         p.AddView(lblSentDateTime, 95dip, Height+25dip, 65%x, 20dip) 'view #0
     End If
  
    Return p
End Sub
 
Top