Android Question CheckBox is not showing correct state when disabled.

Tom_707

Member
Licensed User
When I use a CheckBox and set CheckBox.Enabled = False, it no longer shows the state of the CheckBox (whether it's clicked or not clicked).
As an example, in the following code, the CheckBox first displays as being checked, but then becomes blank (unchecked) when it is disabled:
B4X:
Dim chkTest As CheckBox

chkTest.Checked = True
chkTest.Enabled = False
This is obviously an Android annoyance and not something to do with B4X.

Is there a reason for it working like this?
Is there a way to overcome this, so as to be able to display a Checkbox's state, even when it is disabled?
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
I tested your issue and the code works as expected. The chkb gets checked and then stays checked when disabled. I even tried it with both declarations.
B4X:
'Private chkb As B4XView
    Private chkb As CheckBox

B4X:
chkb.Checked = True
    chkb.Enabled = False

If you are still having trouble, try this code instead:
B4X:
Sub chkb_CheckedChange(Checked As Boolean)
    If Checked Then chkb.Enabled=False
End Sub
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
ive had similar issues. and ive had issues where the checkbox had the wrong theme compared to everything else, being solid black compared to the rest of my theme, including the checked theme.

It is weird, and I do know that it varies by device and android version. android 5 through 7 give me the most fits where 8 and higher does not.
 
Upvote 0

Tom_707

Member
Licensed User
ive had similar issues. and ive had issues where the checkbox had the wrong theme compared to everything else, being solid black compared to the rest of my theme, including the checked theme.

It is weird, and I do know that it varies by device and android version. android 5 through 7 give me the most fits where 8 and higher does not.
I thought that might be the issue.

That when the View's state changes to disabled, it's colour gets changed to the disabled colour. If it's not set correctly, ie. the disabled colour is the same as the background, it will make the View 'invisible'. I'm going to try to use StateListDrawable to fix the issue.

Thanks techknight.
 
Upvote 0
Top