label and button height

marcick

Well-Known Member
Licensed User
Longtime User
Why a label appears higher than a button if both of them have the same height in the designer ?
Marco
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
The default Button layout has a transparent surrounding of several pixels.
If you set the Button.Backgound to ColorDrawable or GradientDrawable the Button will occupy the whole surface.

Best regards.


Klaus,

An old post but, is there a way have Button.SetBackgroundImage(Image) occupy the whole surface? I am trying have a number of buttons join edge to edge.

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Klaus



B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Buttons(8, 8) As Button
   Dim ButtonViews(8,8)
   Dim Tile As Bitmap
   Dim Background As ImageView


Sub Activity_Create(FirstTime As Boolean)
    'Do notSub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Portrait")
 
    Dim width, height As Int
    width = 12.5%x
    height = width
    Tile.Initialize ( File.DirAssets , "Tile.png")

    For x = 0 To 7
        For y = 0 To 7
            Dim btn As Button
            btn.Initialize("button")             'All buttons share the same event sub
            btn.SetBackgroundImage(Tile)
            btn.Gravity = Gravity.FILL
            Activity.AddView(btn, x * width, y * height, width , height)
            Buttons(x, y) = btn                 'store a reference to this view
        Next
    Next
 
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
        End If
       If v Is EditText Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
       If v Is Label Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
    Next

End Sub forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Portrait")
 
    Dim width, height As Int
    width = 12.5%x
    height = width
    Tile.Initialize ( File.DirAssets , "Tile.png")

    For x = 0 To 7
        For y = 0 To 7
            Dim btn As Button
            btn.Initialize("button")             'All buttons share the same event sub
            btn.SetBackgroundImage(Tile)
            btn.Gravity = Gravity.FILL
            Activity.AddView(btn, x * width, y * height, width , height)
            Buttons(x, y) = btn                 'store a reference to this view
        Next
    Next
 
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
        End If
       If v Is EditText Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
       If v Is Label Then
             Dim jo As JavaObject = v
             jo.RunMethod("setPadding", Array As Object(0, 0, 0, 0))
       End If
    Next

End Sub
Atoms.jpg

Regards Roger
 
Last edited:
Upvote 0
Top