Search code examples
propertiesviewdesignercenterbasic4android

Any way to center a view?


I noticed in the properties of the designer you can center the text in a label, etc. did not see anything that allows you to center the view itself.

For example, I looked at the properties for a button. There is Horizontal Alignment and Vertical Alignment for Text Style but I don't see anything like that for Button Properties.

Is there a way to control the button alignment?

Thanks.


Solution

  • You will need to do it programmatically:

    Sub CenterView(v As View, parent As View)
        v.Left = parent.Width / 2 - v.Width / 2
        v.Top = parent.Height / 2 - v.Height / 2
    End Sub
    

    For example:

    CenterView(button1, Activity)