Android Question Resize 12 buttons in a panel based on the Panel size

Discussion in 'Android Questions' started by aaronk, Jun 27, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User


    I am trying to re-size a panel that has 12 buttons in it but when re-sizing the panel I want it to resize the buttons.

    Each of the buttons are square in size and are the same size, like shown in the image below.

    How can I make it so that the buttons resize based on the panel size ?

    So if the panel gets bigger (or smaller) the buttons then get bigger (or smaller) but keeping the spacing between the buttons the same.


    However, if the panel that is holding the buttons is wider then the height like shown below, I want to make the buttons smaller but cantering the buttons since the button needs to remain square in size.


    Anyone know how I can do this ?
    (I have been trying all day and can't seem to work it out.)
  2. MaFu

    MaFu Well-Known Member Licensed User

    Assume we have created a layout named "1" with twelve buttons "Button1" to "Button12".

    #Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #End Region

    Sub Process_Globals
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
    Private Button6 As Button
    Private Button7 As Button
    Private Button8 As Button
    Private Button9 As Button
    Private Button10 As Button
    Private Button11 As Button
    Private Button12 As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    Activity, Button1, 004320dip60dip)
    Activity, Button2, 014320dip60dip)
    Activity, Button3, 024320dip60dip)
    Activity, Button4, 104320dip60dip)
    Activity, Button5, 114320dip60dip)
    Activity, Button6, 124320dip60dip)
    Activity, Button7, 204320dip60dip)
    Activity, Button8, 214320dip60dip)
    Activity, Button9, 224320dip60dip)
    Activity, Button10, 304320dip60dip)
    Activity, Button11, 314320dip60dip)
    Activity, Button12, 324320dip60dip)
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause(UserClosed As Boolean)
    End Sub

    Sub PlaceButton(parent As Panel, btn As Button, row As Int, col As Int, numRows As Int, numCols As Int, distBetween As Int, distOutside As Int)
    Dim width, height, size, startX, startY As Int
        width = (parent.Width - distBetween * (numCols - 
    1) - distOutside * 2) / numCols
        height = (parent.Height - distBetween * (numRows - 
    1) - distOutside * 2) / numRows
        size = 
    Min(width, height)
        startX = parent.Width / 
    2 - (size * numCols + distBetween * (numCols - 1)) / 2
        startY = parent.Height / 
    2 - (size * numRows + distBetween * (numRows - 1)) / 2
        btn.Width = size
        btn.Height = size
        btn.Left = startX + size * col + distBetween * col
        btn.Top = startY + size * row + distBetween * row
    End Sub
    mangojack and DonManfred like this.
  3. aaronk

    aaronk Well-Known Member Licensed User

    Thanks heaps.. It has helped me out a lot and never would of thought of doing it that way, so thank you very much for your help.
  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