Android Question Adding a B4XRadioButton programatically

wimpie3

Well-Known Member
Licensed User
Longtime User
I've tried to create a panel and add a radio button inside by doing this:
B4X:
    Dim contentsPanel As B4XView=XUI.CreatePanel("")
    contentsPanel.SetLayoutAnimated(0, 0, 0, 300dip, 300dip)
    Dim rb1 As B4XRadioButton
    rb1.Initialize(Me,"Radiobutton")
    rb1.Checked=True
    rb1.mHaptic=False
    rb1.OnColor=Colors.Red
    rb1.OffColor=Colors.Blue
    rb1.Text="Radio button"
    rb1.mLabel.TextColor = XUI.Color_Red
    contentsPanel.AddView(rb1.mbase,0,0,100dip,100dip)

but I get the message that rb1 is not initialized. I suspect a B4XRadioButton cannot be added programatically?
 

Alexander Stolte

Expert
Licensed User
Longtime User
This should work:
B4X:
    Dim xpnl_RadioButton As B4XView = xui.CreatePanel("")
    xpnl_RadioButton.SetLayoutAnimated(0, 0, 0, 300dip, 300dip)
    
    Dim m_Properties As Map
    m_Properties.Initialize
    m_Properties.Put("Value",False)
    m_Properties.Put("OnColor",0xFF00D254)
    m_Properties.Put("OffColor",0xFFCACACA)
    m_Properties.Put("HapticFeedback",True)
    
    Dim lbl As Label
    lbl.Initialize("")
    
    Dim rb1 As B4XRadioButton
    rb1.Initialize(Me,"rb1")
    rb1.DesignerCreateView(xpnl_RadioButton,lbl,m_Properties)
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
This should work:
B4X:
    Dim xpnl_RadioButton As B4XView = xui.CreatePanel("")
    xpnl_RadioButton.SetLayoutAnimated(0, 0, 0, 300dip, 300dip)
    
    Dim m_Properties As Map
    m_Properties.Initialize
    m_Properties.Put("Value",False)
    m_Properties.Put("OnColor",0xFF00D254)
    m_Properties.Put("OffColor",0xFFCACACA)
    m_Properties.Put("HapticFeedback",True)
    
    Dim lbl As Label
    lbl.Initialize("")
    
    Dim rb1 As B4XRadioButton
    rb1.Initialize(Me,"rb1")
    rb1.DesignerCreateView(xpnl_RadioButton,lbl,m_Properties)
If I remember correctly, Erel's viewpoint was that this works for now, but it can change in the future.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
If I remember correctly, Erel's viewpoint was that this works for now, but it can change in the future.
As soon as the view gets more designer properties and the developer doesn't add any GetDefault values, the whole thing doesn't work anymore if you don't maintain it manually. Per designer is the simpler variant, but there you must also open and close the layout file for new properties, if the developer has not added GetDefault values for the new properties.
 
Upvote 0
Top