Android Code Snippet Change CheckBox colors

Erel

Administrator
Staff member
Licensed User
1. Add:
B4X:
#AdditionalJar: com.android.support:support-compat
2:
B4X:
Sub SetButtonTintList(View As View, Disabled As Int, Enabled As Int)
   Dim States(2,1) As Int
   Dim sd As StateListDrawable 'ignore
   States(0, 0) = sd.State_Enabled
   States(1, 0) = sd.State_Disabled
   Dim Color(2) As Int = Array As Int(Enabled, Disabled)
   Dim CSL As JavaObject
   CSL.InitializeNewInstance("android.content.res.ColorStateList",Array(States,Color))
   Dim jo As JavaObject
   jo.InitializeStatic("android.support.v4.widget.CompoundButtonCompat")
   jo.RunMethod("setButtonTintList", Array(View, CSL))
End Sub
Usage example:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
   If v Is CheckBox Then
       SetButtonTintList(v, Colors.Red, Colors.Blue)
   End If
Next
Note that it only has effect on Android 5+ devices.
 
Top