B4J Code Snippet Using ToggleButtons to create a Segmented Bar

java_gaLzoErIlc.gif


1. Add a designer script extension, based on @stevel05 code + CSS styling, into B4XMainPage (or any other class):
Code: Copy
'Parameters: SelectedIndex as Int, 2 or more RadioButtons or ToggleButtons
'DesignerScript: {Class}.SetToggleGroup(0,RadioButton1,Radiobutton2)
Public Sub SetToggleGroup(DesignerArgs As DesignerArgs)
    Dim SelectedIndex As Int = DesignerArgs.Arguments.Get(0)
    Dim RBG As JavaObject
    RBG.InitializeNewInstance("javafx.scene.control.ToggleGroup",Null)
    For i = 1 To DesignerArgs.Arguments.Size - 1
        Dim RB As JavaObject = DesignerArgs.GetViewFromArgs(i)
        RB.RunMethod("setToggleGroup",Array(RBG))
        CSSUtils.SetStyleProperty(RB, "-fx-border-color", "#d9dee5")
        If i = 1 Then
            CSSUtils.SetStyleProperty(RB, "-fx-background-radius", "5 0 0 5")
            CSSUtils.SetStyleProperty(RB, "-fx-border-radius", "5 0 0 5")
            CSSUtils.SetStyleProperty(RB, "-fx-border-width", "1 0 1 1")
        Else If i = DesignerArgs.Arguments.Size - 1 Then
            CSSUtils.SetStyleProperty(RB, "-fx-background-radius", "0 5 5 0")
            CSSUtils.SetStyleProperty(RB, "-fx-border-radius", "0 5 5 0")
            CSSUtils.SetStyleProperty(RB, "-fx-border-width", "1")
        Else
            CSSUtils.SetStyleProperty(RB, "-fx-border-width", "1 0 1 1")
            CSSUtils.SetStyleProperty(RB, "-fx-border-radius", "0")
        End If
    Next
    If SelectedIndex < 0 Or SelectedIndex > DesignerArgs.Arguments.Size - 2 Then
        SelectedIndex = 0
    End If
    DesignerArgs.GetViewFromArgs(SelectedIndex + 1).As(JavaObject).RunMethod("setSelected",Array(True))
End Sub

2. Add to designer script:
Code: Copy
B4XMainPage.SetToggleGroup(0, tglScale, tglCoordinage, tglOrientation)

3. Courtesy of ChatGPT, add to a CSS file:
Code: Copy
.toggle-button {
    -fx-background-color: #f7f8fa;
    -fx-background-insets: 0;
    -fx-background-radius: 0;
    -fx-padding: 4 12;
    -fx-text-fill: #243044;
}

.toggle-button:hover {
    -fx-background-color: #e2e8f0;
}

.toggle-button:selected {
    -fx-background-color: #dceaff;
    -fx-text-fill: #173c70;
}

.toggle-button:focused {
    -fx-border-color: #5278ad;
}

.toggle-button:disabled {
    -fx-background-color: #f0f2f5;
    -fx-text-fill: #526174;
    -fx-opacity: 1;
}
.toggle-button:selected:disabled {
    -fx-background-color: #dceaff;
You can keep a single CSS file with all rules and load it with:
Code: Copy
B4XPages.GetNativeParent(Me).Stylesheets.Add(File.GetUri(File.DirAssets, "your_css_file.css"))

4. Set the EventName of the ToggleButtons to be the same and handle the SelectedChange event:
Code: Copy
Private Sub tglMode_SelectedChange(Selected As Boolean)
    Sender.As(B4XView).Enabled = Not(Selected) 'prevents the selected button from being unselected
    If Selected Then
        If Sender = tglScale Then
            '...
        End If
    End If
End Sub
 
Last edited:
Back
Top