Android Question label alignment inside a panel inside a customlistview

Discussion in 'Android Questions' started by leitor79, Apr 6, 2015.

  1. leitor79

    leitor79 Active Member Licensed User


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

    This is the basic code I have:

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

    Sub CreatePanel(MyText As StringAs Panel

    Dim l As Label
    Dim p As Panel
    Dim jo As JavaObject = l
    "setPadding"Array As Object(10101010))
    Dim su As StringUtils
        h=su.MeasureMultilineTextHeight(l, l.text)+
    20 '20 is the padding
        l.Height = h
    Return p
    End Sub
    The first thing I did was something like this...

    ...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!
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can set the text alignment with:
    l.Gravity = Gravity.RIGHT
  3. leitor79

    leitor79 Active Member Licensed 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.


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

    Thank you very much!
  4. Erel

    Erel Administrator Staff Member Licensed User

    Don't use the -2 approach. You can use Canvas.MeasureStringWidth to find the width and then set the position to be 100%x - width.
  5. leitor79

    leitor79 Active Member Licensed User

    Thank you very much, Erel. It worked.
  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