Android Question RadioButton without circle but with contour when pressed

ThePuiu

Active Member
Licensed User
Longtime User
Hi,
I need a B4A and B4I solution for a RadioButton without a circle but with a contour when selected (or another distinctive sign for the selected button)
Thank you!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I was playing around with the cross platform xCLV and managed to create a reasonable and versatile vertical radio button group. It may work for you too.

Note I used B4J, but it should work in B4A and B4i. (without JFX)

B4X:
'Designer file "1":  add custom listview = anchors left, top; layout (0, 20, 330, 480); divider size = 0; border width = 0
Sub Process_Globals
    Private fx As JFX
    Private xui As XUI
    Private MainForm As Form
    Private CustomListView1 As CustomListView
    Private mIconFont, defaultFont As B4XFont
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    mIconFont = xui.CreateMaterialIcons(30)
    defaultFont = xui.CreateDefaultFont(18)
    CustomListView1.DefaultTextBackgroundColor = xui.Color_White
    CustomListView1.sv.color = xui.Color_Transparent
    CustomListView1.PressedColor = xui.Color_White
    For i = 0 To 6
        CustomListView1.Add(addRadioItem("Item " & i, 30), "")
    Next
    CustomListView1.sv.SetLayoutAnimated(0, 0, 0, CustomListView1.GetBase.Width, CustomListView1.Size * CustomListView1.GetPanel(0).Height)
End Sub

Sub addRadioItem(text As String, h As Int) As B4XView
    Dim h2 As Int = h / 2
    Dim h3 As Int = 2 * h / 3
    Dim w As Int = CustomListView1.GetBase.Width
    Dim offset As Int = h + h3 + 5dip
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, w, 1.8 * h)
    pnl.Color = xui.Color_White
    Dim mark As B4XView = makeMark
    turnMarkOff(pnl, mark)
    pnl.AddView(mark, h3, h2, h, h)
    Dim caption As Label: caption.Initialize("")
    Dim xcaption As B4XView = caption
    xcaption.font = defaultFont
    xcaption.Text = text
    pnl.AddView(caption, offset, h2, w - offset, h)
    Return pnl
End Sub

Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    For j = 0 To 6
        Dim pnl As B4XView = CustomListView1.GetPanel(j)
        Dim mark As B4XView = pnl.GetView(0)
        If j = Index Then
            If pnl.Tag Then turnMarkOff(pnl, mark) Else turnMarkOn(pnl, mark)
        Else
            turnMarkOff(pnl, mark)
        End If
    Next
End Sub

Sub makeMark As B4XView
    Dim nativeMark As Label: nativeMark.Initialize("")
    Dim mark As B4XView = nativeMark
    mark.Font = mIconFont
    Return mark
End Sub

Sub turnMarkOn(pnl As B4XView, mark As B4XView)
    mark.Text = Chr(0xE8DC)
    mark.TextColor = xui.Color_RGB(0, 160, 0)
    pnl.Tag = True
End Sub

Sub turnMarkOff(pnl As B4XView, mark As B4XView)
    mark.Text = Chr(0xE8DB)
    mark.TextColor = xui.Color_RGB(180, 180, 255)
    pnl.Tag = False
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The android version is the same with a small difference in dimensions.

B4X:
'Designer file "1":  add custom listview = anchors left, top; layout (0, 20, 330, 480); divider size = 0; border width = 0
Sub Process_Globals
    Private xui As XUI
    Private mIconFont, defaultFont As B4XFont
End Sub

Sub Globals
    Private CustomListView1 As CustomListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    mIconFont = xui.CreateMaterialIcons(30)
    defaultFont = xui.CreateDefaultFont(18)
    CustomListView1.DefaultTextBackgroundColor = xui.Color_White
    CustomListView1.sv.color = xui.Color_Transparent
    CustomListView1.PressedColor = xui.Color_White
    For i = 0 To 6
        CustomListView1.Add(addRadioItem("Item " & i, 30dip), "")
    Next
    CustomListView1.sv.SetLayoutAnimated(0, 0, 0, CustomListView1.GetBase.Width, CustomListView1.Size * CustomListView1.GetPanel(0).Height)
End Sub
 
Upvote 0
Top