Android Question label alignment inside a panel inside a customlistview

leitor79

Active Member
Licensed User
Longtime User
Hi,

I need to align a label to the right of the panel to add into a customlistview.

This is the basic code I have:

B4X:
p=CreatePanel(MyText)
lstChat.Add(p,h,MyObject)

h is ant int, set in CreatePanel sub (I don't have a better idea):

B4X:
Sub CreatePanel(MyText As String) As Panel

    Dim l As Label
    Dim p As Panel
   
    p.Initialize("")
    l.Initialize("")
   
    l.Text=MyText
    l.TextSize=14
    l.Typeface=Typeface.DEFAULT
    l.TextColor=Colors.black   
    l.Color=Colors.Green
   
    Dim jo As JavaObject = l
    jo.RunMethod("setPadding", Array As Object(10, 10, 10, 10))
   
    p.AddView(l, 0,0, 80%x,0) 
   
    Dim su As StringUtils
    h=su.MeasureMultilineTextHeight(l, l.text)+20 '20 is the padding
    l.Height = h
    l.Width=-2
 
    Return p
   
End Sub

The first thing I did was something like this...

B4X:
l.Left=100%x-l.Width

...but the panel's "width" property is -2 even after the panel is added to the list (got with GetAllViewsRecursive method)

I've tried using javaobject ("Gravity", "setGravity"), but it seems these property/method doesn't exist (or I don't know how to use them)

Bonus track:

I also want to adjust the label width to adapt the text length (-2, ok) but to a maximum of 80%x. If the text length exceeds 80%x, I want the label to have that width (80%x) and grow in height. I've thought comparing the label width with the 80%x, and then assign -2 or 80%x, but again, the width is still -2.

Also, I've tried to look for the way to get the width of a text, as I get the height, but I couldn't find a way except for the "-2". I think with the width I could adjust lengths and alignments manually...


Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Hi Erel, thanks for your answer.

I've explained myself wrong, and I'm attaching a picture to make it clear.

I don't want to align the text inside the label (I mean; I want but I can do that), I want to align the label within the panel. The "green" is the label, the "gray" is the customlistview background.

mbn7k1.png


The "desired behaviour" I've got here was hardcoding the width value; I need the label width to auto size.

Thank you very much!
 
Upvote 0
Top