Android Question Checkbox Checked Color

Reviewnow

Active Member
Licensed User
Longtime 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
Longtime User
To use programatically it requires a ColorStateList As documented here: http://developer.android.com/refere...nTintList(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:
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
Stevel05,


Tried this it works great thanks so much
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime 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.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Thank you if you know I misinterpreted something. I am using API 22, javaObject 2.05, B4A 6.3.
Weird - I copied your code-snippet "as is" into an empty project and it works just fine (tested with API 22 and 23).
I even modified Steve's code for use with RadioButtons and this works too.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…