B4J Question Adding This Sub To DesignerUtils.b4xlib

B4XDev

Member
Licensed User
I'd like to add this Sub to the DesignerUtils.b4xlib:

New DesignerUtils.b4xlib Sub:
'Justifies the controls evenly to the left
'Parameters: Panel, Starting indent, Minimum gap between controls
Public Sub PackControlsLeft (DesignerArgs As DesignerArgs)
    Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
    If Panel.IsInitialized = False Then Return
    Dim startX as Int = DesignerArgs.Arguments.Get(1)
    Dim Gap As Int = DesignerArgs.Arguments.Get(2)
    For i = 0 To Panel.NumberOfViews - 1
        Dim v As B4XView = Panel.GetView(i)
        v.SetLayoutAnimated(0, startX, v.Top, v.Width, v.Height)
        startX = startX + v.Width + Gap
    Next
End Sub

The problem is, I need to be able to get the maximum width of the control to contain all the text. Does v.Width cover that in the above Sub, or will that only acquire the Width that has been set in the Designer/code for that view?
 

stevel05

Expert
Licensed User
Longtime User
Last edited:
Upvote 2

B4XDev

Member
Licensed User
I can't diagnose the error I'm getting with this test project. Can you see what I'm doing wrong?

I feel like it's gotta be close, but I don't know for sure! ?

It's supposed to be spacing out radio buttons to their string text width.
 

Attachments

  • TestDesignerExt-001.zip
    4.1 KB · Views: 65
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are passing all of the Views to MeasureTextWidth, but one of them is a Pane and has no text property, which is causing the issue.

Unfortunately it is not easy to do this kind of thing with Customviews as they are made up of more than 1 view. This code is close, but probably needs more work and is specific to B4xRadioButtons.

B4X:
'Justifies the full-width controls (based on text labels) evenly to the left
'Parameters: Panel, Starting indent, Minimum gap between controls
Private Sub PackControlsLeft (DesignerArgs As DesignerArgs)
    Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
    If Panel.IsInitialized = False Then Return
    Dim startX As Int = DesignerArgs.Arguments.Get(1)
    Dim Gap As Int = DesignerArgs.Arguments.Get(2)
    For i = 0 To Panel.NumberOfViews - 1
        Dim v As B4XView = Panel.GetView(i)
        If V Is Pane Then
            If V.tag Is B4XRadioButton Then            'Identifies the customviews type
                Log("Found")
                Dim Lbl As B4XView = v.GetView(1)    'Get the label from the Custom view
                Dim Offset As Int = Lbl.Left        'Get it's left position so we know how wide the indicator is
                Dim tw As Int = MeasureTextWidth(Lbl) + 10 'Add 10 to allow for the labels padding.  Could probably find the exact value if needed
                'Adjust the layout of the Custom views base
                v.SetLayoutAnimated(0, startX, V.Top, tw + Offset, V.Height)
                'Adjust the layout of the label
                Lbl.SetLayoutAnimated(0, Offset, 0, tw, Lbl.Height)
                startX = startX + tw + Gap + Offset
            End If
        End If
     
    Next
End Sub
 
Upvote 0

B4XDev

Member
Licensed User
You are passing all of the Views to MeasureTextWidth, but one of them is a Pane and has no text property, which is causing the issue.

I thought I was passing a Pane that contains B4XRadioButtons. Did I fail to do that properly? Or, I guess like you said, CustomViews are beasts in their own domain.

Maybe it would be better to pass the actual controls I want lined up...?

Regardless, your code works for me! Doesn't matter the label applied, the controls get justified left and line up one after the other, respecting the width of the label text.

Thank you! I will further study this code and see if I can come up with something more generic for all View cases...
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Thank you! I will further study this code and see if I can come up with something more generic for all View cases..
It would be straight forward, but time consuming, to include native views, or even CustomViews, just to line them up using the view or base pane. The problem comes from moving / sizing views within the CustomView as in sizing the label to fit the text, each will be different. You would need to write code for each separate CustomView.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…