Android Question Checkbox Checked Color

Discussion in 'Android Questions' started by Reviewnow, Dec 28, 2015.

  1. Reviewnow

    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"
     
  2. stevel05

    stevel05 Expert Licensed 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.

    Code:
    '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,1As Int

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


        
    Dim Color(3As 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:
    Code:
    SetColorTintList(CheckBox1,Colors.Red,Colors.White,Colors.Gray)
     
    Last edited: Dec 29, 2015
    FernandoMassa, Seneca, wroyw and 6 others like this.
  3. Reviewnow

    Reviewnow Active Member Licensed User

    Stevel05,


    Tried this it works great thanks so much
     
    Last edited: Dec 28, 2015
  4. moster67

    moster67 Expert Licensed User

    Works nice - just what I needed. Thanks!
     
  5. Mahares

    Mahares Well Known Member Licensed User

    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

    Code:
    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,1As Int
        States(
    0,0) = 16842912   'Checked
        States(1,0) = 16842910    'Enabled
        States(2,0) = -16842910 'Disabled
        Dim Color(3As 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.
     
  6. moster67

    moster67 Expert Licensed User

    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.
     
    Mahares and DonManfred like this.
  7. DonManfred

    DonManfred Expert Licensed User

    it is working for me also.
     
    Mahares and moster67 like this.
  8. Mahares

    Mahares Well Known Member Licensed User

    Thank you @DonManfred & @moster67
    it worked on OS 6.01 device, but earlier I was using a tablet with OS 4.2.2 where it displayed the error.
     
  9. DonManfred

    DonManfred Expert Licensed User

    OS 4.2.2 is api 19 or so....
    You need OS with api 21+
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice