Android Question Checkbox Checked Color

Reviewnow

Active Member
Licensed User
android has a property available > api 21 that you can change the checked color of a checkbox how can I use it in b4a android:buttonTint="@Color/YOUR_CHECKMARK_COLOR_HERE"
 

stevel05

Expert
Licensed User
To use programatically it requires a ColorStateList As documented here: http://developer.android.com/reference/android/widget/CompoundButton.html#setButtonTintList(android.content.res.ColorStateList)

You can set it something like this.

B4X:
'Pass the Checkbox object and Colors int values to set for each state.
Sub SetColorTintList(CB As CheckBox,Checked As Int,Enabled As Int,Disabled As Int)

    Dim States(3,1) As Int

    States(0,0) = 16842912   'Checked
    States(1,0) = 16842910    'Enabled
    States(2,0) = -16842910 'Disabled


    Dim Color(3) As Int = Array As Int(Checked,Enabled,Disabled)

    Dim CSL As JavaObject
    CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,Color))
    Dim CB1 As JavaObject = CB
    CB1.RunMethod("setButtonTintList",Array As Object(CSL))

End Sub
And use it something like:
B4X:
SetColorTintList(CheckBox1,Colors.Red,Colors.White,Colors.Gray)
 
Last edited:

Mahares

Well Known Member
Licensed User
Works nice - just what I needed. Thanks!
Simply out of curiosity, I have tried it in a small project as shown below, but got this error:
java.lang.RuntimeException: Method: setButtonTintList Not found in: android.widget.CheckBox

B4X:
Sub Globals
    Dim CheckBox1 As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
    CheckBox1.Initialize("check")
    Activity.AddView(CheckBox1,30%x,40%y,20%x,20%y)
    SetColorTintList(CheckBox1,Colors.Red,Colors.White,Colors.Gray)
End Sub

Sub SetColorTintList(CB As CheckBox,Checked As Int,Enabled As Int,Disabled As Int)
    Dim States(3,1) As Int
    States(0,0) = 16842912   'Checked
    States(1,0) = 16842910    'Enabled
    States(2,0) = -16842910 'Disabled
    Dim Color(3) As Int = Array As Int(Checked,Enabled,Disabled)
    Dim CSL As JavaObject
    CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,Color))
    Dim CB1 As JavaObject = CB
    CB1.RunMethod("setButtonTintList",Array As Object(CSL))
End Sub
Thank you if you know I misinterpreted something. I am using API 22, javaObject 2.05, B4A 6.3.
 
Top