Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private cb As CheckBox
Private fxCB As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.Show
' Access to native JavaFX CheckBox
fxCB = cb
fxCB.RunMethod("setAllowIndeterminate", Array(True))
fxCB.RunMethod("setIndeterminate", Array(True)) ' Initial state: undetermined
End Sub
Sub cb_MouseClicked (EventData As MouseEvent)
Dim isIndeterminate As Boolean = fxCB.RunMethod("isIndeterminate", Null)
Dim isSelected As Boolean = cb.Checked
' Switch between the three states
If isIndeterminate Then
fxCB.RunMethod("setIndeterminate", Array(False))
cb.Checked = True
Else If isSelected Then
cb.Checked = False
Else
fxCB.RunMethod("setIndeterminate", Array(True))
End If
End Sub
Thank you for the answer.Yes, it is possible in B4J to have a checkbox with three states, including an indeterminate (or "greyed out") state.
This relies on the use of the CheckBox component of JavaFX, which supports a mode called "tri-state".
...
Private CheckState As Int = 0 ' 0 = undetermined, 1 = checked, 2 = unchecked
Private CheckBox1 As CheckBox
....
....
Private Sub CheckBox1_CheckedChange(Checked As Boolean)
CheckState = (CheckState + 1) Mod 3
Select CheckState
Case 0
CheckBox1.Checked = False
CheckBox1.Text = "Undetermined"
CheckBox1.Color = Colors.Gray ' To visually indicate
Case 1
CheckBox1.Checked = True
CheckBox1.Text = "Checked"
CheckBox1.Color = Colors.Green
Case 2
CheckBox1.Checked = False
CheckBox1.Text = "Unchecked"
CheckBox1.Color = Colors.Red
End Select
End Sub
This... "wonder" tooNote that B4XRadioButton (XUI Views) has a disabled state which looks a bit different than the other two states.
Thank you!Sorry, I didn't notice that it's in b4a.
Here is a b4a version.
States(0, 0) = sd.State_Checked
States(1, 0) = sd.State_Unchecked
Dim Color(2) As Int = Array As Int(Colors.Gray, Colors.DarkGray)
I've seen this, as well as the AS_CheckBoxAdvanced, but unfortunately I did not find it useful for my case.[B4X] [XUI] AS Checkbox
This is a simple cross platform Checkbox. If you need a checkbox with text, then check out the AS_CheckBoxAdvanced I spend a lot of time in creating views, like this and to create a high quality view cost a lot of time. If you want to support me and further views, then you can do it here by...www.b4x.com
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim jo As JavaObject = CheckBox1
' Change the color of the check mark
jo.RunMethod("setButtonTintList", Array As Object(CreateColorStateList(Colors.Red)))
End Sub
Sub CreateColorStateList(Color As Int) As Object
Dim states(1,1) As Int
states(0,0) = 16842910 'android.R.attr.state_enabled
Dim mColors() As Int = Array As Int(Color)
Dim jo As JavaObject
jo.InitializeStatic("android.content.res.ColorStateList")
Return jo.RunMethod("valueOf", Array(mColors(0)))
End Sub
Sub ImageView1_Click
isChecked = Not(isChecked)
If isChecked Then
ImageView1.Bitmap = LoadBitmap(File.DirAssets, "checked.png")
Else
ImageView1.Bitmap = LoadBitmap(File.DirAssets, "unchecked.png")
End If
End Sub
Dim cs As CSBuilder
cs.Initialize.Color(Colors.Blue).Typeface(Typeface.SERIF).Append("Accept the conditions").PopAll
CheckBox1.Text = cs
A question: can we make the square round like a circle?If you want to change the appearance of the checkbox in B4A using JavaObject, you can access native Android properties not directly exposed by B4A.
Here's an example to change the color of the shell itself (the "check mark") without affecting the background
JavaObject:Private Sub B4XPage_Created (Root1 As B4XView) Root = Root1 Root.LoadLayout("MainPage") Dim jo As JavaObject = CheckBox1 ' Change the color of the check mark jo.RunMethod("setButtonTintList", Array As Object(CreateColorStateList(Colors.Red))) End Sub Sub CreateColorStateList(Color As Int) As Object Dim states(1,1) As Int states(0,0) = 16842910 'android.R.attr.state_enabled Dim mColors() As Int = Array As Int(Color) Dim jo As JavaObject jo.InitializeStatic("android.content.res.ColorStateList") Return jo.RunMethod("valueOf", Array(mColors(0))) End Sub
View attachment 166315
Actually, in the previous post, I've mentioned that I already know how to change the color of the checkmark, with the following code:If you want to change the appearance of the checkbox in B4A using JavaObject, you can access native Android properties not directly exposed by B4A.
Here's an example to change the color of the shell itself (the "check mark") without affecting the background
JavaObject:Private Sub B4XPage_Created (Root1 As B4XView) Root = Root1 Root.LoadLayout("MainPage") Dim jo As JavaObject = CheckBox1 ' Change the color of the check mark jo.RunMethod("setButtonTintList", Array As Object(CreateColorStateList(Colors.Red))) End Sub Sub CreateColorStateList(Color As Int) As Object Dim states(1,1) As Int states(0,0) = 16842910 'android.R.attr.state_enabled Dim mColors() As Int = Array As Int(Color) Dim jo As JavaObject jo.InitializeStatic("android.content.res.ColorStateList") Return jo.RunMethod("valueOf", Array(mColors(0))) End Sub
View attachment 166315
States(0, 0) = sd.State_Checked
States(1, 0) = sd.State_Unchecked
Dim Color(2) As Int = Array As Int(Colors.Gray, Colors.DarkGray)
Dim jo As JavaObject = CheckBox1
jo.RunMethod("setButtonTintList", Array As Object(Null))
You can wrap your CheckBox in a Panel or use a Label with a custom background.A question: can we make the square round like a circle?
I tried the above code but it has no effect (the checkbox appearance didn't change).Simply pass it a null value.
JavaObject:Dim jo As JavaObject = CheckBox1 jo.RunMethod("setButtonTintList", Array As Object(Null))
Dim States(2, 1) As Int
Dim SLD As StateListDrawable
SLD.Initialize
States(0, 0) = SLD.State_Checked
States(1, 0) = SLD.State_Unchecked
Dim Color(2) As Int = Array As Int(Colors.Gray, Colors.DarkGray)
Dim CSL As JavaObject
CSL.InitializeNewInstance("android.content.res.ColorStateList", Array(States, Color))
Dim CBC As JavaObject = CheckBox1
CBC.RunMethod("setButtonTintList", Array(CSL))
Dim jo As JavaObject = CheckBox1
jo.RunMethod("setButtonTintList", Array As Object(Null))
I have mentioned this link in the post #7.
this code works correctly.And then I used the code you suggested to restore the appearance to default state, but it has no effect.
That looks good but I didn't see any code example in the given link?
Quien necesita un checkbox Personalizado.
Pronto publicare un checkbox de vista personalizada para B4A, B4J y B4I, con muchas opciones de apariencias y animación personalizables. no utiliza imagenes solo IconFont. (para poder cambiar tamaño, color, color de fondo, agregar texto, animaciones, etc) dejo un ejemplo: Coloreado en Azul:www.b4x.com