Android Question Fourth state 'checkbox'

Tomek92

Member
Licensed User
Should I try like here?? There are no bugs in B4A in the code below, but it doesn't work.
B4X:
Sub SetColorTintList(CB As CheckBox,Checked As Int,Enabled As Int,Disabled As Int)
    Dim States(3,1) As Int

    Dim v As B4XView = CB            ' my shift
    Dim sw As B4XSwitch = v.Tag        ' my shift
        
    If CB.Enabled = False Then        ' my shift
        sw.Enabled = False            ' my shift
    End If                            ' my shift

    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

I have yet tried to initialize StateListDrawable. But still the feature is setting the colors wrong, or I am doing it wrong:
B4X:
Sub SetColorTintList(CB As CheckBox,Checked As Int,Unchecked As Int,Enabled As Int,Disabled As Int)
    Dim States(4,1) As Int
    Dim sd As StateListDrawable
    
    sd.Initialize
    
    States(0,0) = sd.State_Checked       'Checked
    States(1,0) = sd.State_Unchecked    'Unchecked
    States(2,0) = sd.State_Enabled        'Enabled
    States(3,0) = sd.State_Disabled     'Disabled,

    Dim Color(4) As Int = Array As Int(Checked,Unchecked,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
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
You need to load the layout using the designer. Select XUI Views first in the Libraries Panel.
In designer select Add View, then custom views, then B4XSwitch. Make color choices in designer.
Use designer tools to generate the switch's definition and event handler.

In your code you can enable and disable, set or reset the value.

B4X:
    Root.LoadLayout("testswitch")
    B4XSwitch1.Value = True
    B4XSwitch1.Enabled = False
 
Last edited:
Upvote 0

Tomek92

Member
Licensed User
Oh I didn't know there are custom views in the designer if I choose the B4X view library. At first I didn't understand what was going on. Thank you for pointing me.
 
Upvote 0

Tomek92

Member
Licensed User
Ok, I use the xui view library, but now I have a problem how to use the enable function, e.g. seekbar to turn the seekbar enable and disable in code.
From what I found out, it should be done like this:
B4X:
B4XSeekBar1.mBase.GetView(1).Enabled = False

but it doesn't work and app crash.
 
Upvote 0

Tomek92

Member
Licensed User
I found a way:
B4X:
Sub EnableDisable_seekbar( enabledArg As Boolean )
    For Each v As B4XView In sbLightPage3.mBase.GetAllViewsRecursive    'wyłączenie seekbara
        v.Enabled = enabledArg                                            'wyłączenie seekbara
    Next
End Sub

But I have no idea how to change the color when the seekbar is disabled. I try so but it doesn't work:
B4X:
    If enabledArg = False Then
        sbLightPage3.Color1 = Colors.LightGray
        sbLightPage3.Color2 = Colors.LightGray
        sbLightPage3.ThumbColor = Colors.LightGray
    End If

Is there any way?
 
Upvote 0
Top