Android Question Centering text in label programmatically

Discussion in 'Android Questions' started by Scotter, Dec 29, 2018.

  1. Scotter

    Scotter Active Member Licensed User

    Hi -

    In the designer I've set the padding, horiz align to "center_horizontal", and vert align to "center_vertical".
    And in my activity_create:
    Code:
    '......................padding: left, top, right, bottom

        lblCard1.Padding = 
    Array As Int(10dip,10dip,10dip,10dip)
        lblCard1.Gravity=
    Gravity.CENTER_HORIZONTAL
        lblCard1.Gravity=
    Gravity.CENTER_VERTICAL
        lblCard1.TextSize=
    34
    But I get left justified when I run the program.
    Ideas?
     
  2. Scotter

    Scotter Active Member Licensed User

    Is this a bug in 8.50 or am I missing something?
     
  3. mangojack

    mangojack Well-Known Member Licensed User

    If you are setting these properties in Designer ... there is no need to call them again in code.

    Removing the lines from Activity_Create solves the problem .. although I can't explain why the problem is created by these lines.

    Maybe someone can explain / expand on this later.
     
    Scotter likes this.
  4. Scotter

    Scotter Active Member Licensed User

    OMG that worked!
     
  5. Scotter

    Scotter Active Member Licensed User

    @Erel can you please look at this thread?
    I'm wondering if this is a bug in 8.50?
    Thanks!
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Best solution is to use B4XView
    Code:
    Dim lblCard1 As B4XView 'in Globals

    lblCard1.SetTextAlignment(
    "CENTER""CENTER")
    Or:
    Code:
    lblCard1.Gravity = Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL)
    Setting the gravity twice overwrites the previous value.
     
    Didier9 and BillMeyer like this.
  7. klaus

    klaus Expert Licensed User

    Or:
    Code:
    lblCard1.Gravity = Gravity.CENTER
     
    Didier9 and Erel like this.
  8. Didier9

    Didier9 Active Member Licensed User

    Thank you, I had come across this unexpected behavior. Now I know how to deal with it!
     
Loading...
  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