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".
b4x:
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
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".
...
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...
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...
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
If you want a completely different style (e.g. an icon instead of the standard box), you can replace the CheckBox with an ImageView + Label, and manage the state manually.
B4A:
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
and use a CSBuilder to style the text
B4A:
Dim cs As CSBuilder
cs.Initialize.Color(Colors.Blue).Typeface(Typeface.SERIF).Append("Accept the conditions").PopAll
CheckBox1.Text = cs
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
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
You can wrap your CheckBox in a Panel or use a Label with a custom background.
This gives a round visual effect, even though the CheckBox itself remains square inside.
B4A does not allow you to directly modify the checkmark drawable without using Java code or an external library.
The checkmark is defined by the Android system. To make it round, you need to change the drawable used by the CheckBox.
This requires creating an XML file in /res/drawable with a custom circle and using a JavaObject to apply this drawable.
I tried the above code but it has no effect (the checkbox appearance didn't change).
Maybe something I've missed?
I used the following code to change the checkbox appearance, it works OK.
B4X:
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))
And then I used the code you suggested to restore the appearance to default state, but it has no effect.
B4X:
Dim jo As JavaObject = CheckBox1
jo.RunMethod("setButtonTintList", Array As Object(Null))
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:
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: