androidandroid-checkboxandroid-event

Disable Android checkbox listener


I am working on a data collection application that while in an active state locks the screen to prevent errant user interaction. I would like to "lock" the checkbox while in this state to prevent the user from checking or unchecking the box. While other buttons on the screen still "click" when in this state, (listener is active) their events are not executed when the boolean bLockedScreen == true. I'd like for my boolean flag (bLockedScreen) when true to disable the check box listener.

What is the best way to go about doing this? TIA

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
            ...
            ckbxVerbose = (CheckBox) v.findViewById(R.id.ckbxVerbose);
            return v;
}
public void verbose(){
    if(ckbxVerbose.isChecked()){
        //do something
}

Update: Resolved this by putting,

 ckbxVerbose.setEnabled(false);

in the method that set the boolean flag to disable screen widgets. Will likely do this as well with the other buttons that are inactive while data is being collected. Thank you Prince Ali for your answer.


Solution

  • You can disable or enable the CheckBox by calling setEnabled and setting its value to false or true. Here is a minimal working example involving 2 CheckBoxes, if the first CheckBox is checked, then disable the second one, otherwise enable it:

    ckbxVerbose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                        if ( isChecked ) {
                            ckbxVerbose2.setEnabled(false);
                        } else {
                            ckbxVerbose2.setEnabled(true);
                        }
                    }
                }
            );
    

    Another example can be:

    CharSequence str = txt.getText();
            if ( str.equals("Checked") ) {
                ckbxVerbose2.setEnabled(false);
            } else {
                ckbxVerbose2.setEnabled(true);
            }